Java 从主要包含空值的 Comparables 列表中获取最小值和最大值的最佳方法是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/369383/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 13:54:40  来源:igfitidea点击:

What is the best way to get min and max value from a list of Comparables that main contain null values?

javacollectionscomparable

提问by user2427

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():

在评论之后,我也尝试过 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?

你对那个怎么想的?

采纳答案by Pyrolistical

What's wrong with Collections.max?

Collections.max 有什么问题?

And why do you care about null safety? Are you sure you want to allow nulls to be in your Collection?

你为什么关心空安全?您确定要在您的集合中允许空值吗?

回答by Marc Novakowski

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.

如果你真的需要从结果中排除“null”,并且你不能阻止它出现在你的数组中,那么也许你应该用一个简单的循环遍历数组并跟踪“min”和“max” " 在不同的变量中。您仍然可以在每个对象上使用“compare()”方法将其与当前的“min”和“max”值进行比较。这样,您可以添加自己的代码来检查空值并忽略它们。

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;
}