views:

1066

answers:

2

I think in 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 safety, wich is something I want too.

Do you know a better way to solve this problem?

EDIT: I really want to this function to be null safety.

I try this for min() thanks to the comments:

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?

+8  A: 

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?

Pyrolistical
+2  A: 

If you really need to exclude "null" from the result, and you can't prevent it from being in your array, then maybe you should just iterate through the array with a simple loop and keep track of the "min" and "max" in separate variables. You can still use the "compare()" method on each object to compare it with your current "min" and "max" values. This way, you can add your own code for checking for nulls and ignoring them.

EDIT: here's some code to illustrate what I'm talking about. Unfortunately there is an edge case you need to consider - what if all of the arguments passed in are null? What does your method return?

public static <T extends Comparable<T>> T minOf(T...ts){
 T min = null;
 for (T t : ts) {
  if (t != null && (min == null || t.compareTo(min) < 0)) {
   min = t;
  }
 }
 return min;
}

public static <T extends Comparable<T>> T maxOf(T...ts){
 T max = null;
 for (T t : ts) {
  if (t != null && (max == null || t.compareTo(max) > 0)) {
   max = t;
  }
 }
 return max;
}
Marc Novakowski