I have an ArrayList which stores 0...4 Dates.
The amount of Dates in the list depends on a Business logic.
How can I get the earliest date of this list? Of course I can build iterative loops to finally retrieve the earliest date. But is there a 'cleaner'/ quicker way of doing this, especially when considering that this list can grow on a later perspective?
java.util.Date
implements Comparable<Date>
, so you can simply use:
Date minDate = Collections.min(listOfDates);
This relies on there being at least one element in the list. If the list might be empty (amongst many other approaches):
Optional<Date> minDate = listOfDates.stream().min(Comparator.naturalOrder());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With