I am thinking about something like this:
public static <T extends Comparable<T>> T minOf(T...ts){
SortedSet<T> set = new TreeSet<T>(Arrays.asList(ts));
return set.first();
}
public static <T extends Comparable<T>> T maxOf(T...ts){
SortedSet<T> set = new TreeSet<T>(Arrays.asList(ts));
return set.last();
}
But is not null safe, which is something I want too.
Do you know a better way to solve this problem?
EDIT:
After the comments I have also tried min():
public static <T extends Comparable<T>> T minOf(T...ts){
return Collections.min(Arrays.asList(ts), new Comparator<T>(){
public int compare(T o1, T o2) {
if(o1!=null && o2!=null){
return o1.compareTo(o2);
}else if(o1!=null){
return 1;
}else{
return -1;
}
}});
}
What do you think of that?
MAX ignores any null values. MAX returns NULL when there is no row to select. For character columns, MAX finds the highest value in the collating sequence.
You can use COALESCE() along with aggregate function MAX() for this.
MIN ignores any null values. With character data columns, MIN finds the value that is lowest in the sort sequence.
What's wrong with Collections.max?
And why do you care about null safety? Are you sure you want to allow nulls to be in your Collection?
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