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

发布于 2024-07-10 03:42:54 字数 931 浏览 11 评论 0原文

我正在考虑这样的事情:

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

但不是空安全的,这也是我想要的。

您知道解决这个问题的更好方法吗?

编辑:

在评论之后我也尝试过 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;  
            }
        }});
}

你对此有何看法?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情愿 2024-07-17 03:42:54

集合有什么问题.max

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

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?

吾家有女初长成 2024-07-17 03:42:54

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

编辑:这里有一些代码来说明我在说什么。 不幸的是,您需要考虑一种边缘情况 - 如果传入的所有参数都为空怎么办? 你的方法返回什么?

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

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;
}
亢潮 2024-07-17 03:42:54

不应该实现Comparable来接受null,因为它破坏了接口的契约。

来自 https://docs.oracle.com/javase /7/docs/api/java/lang/Comparable.html

请注意,null 不是任何类的实例,即使 e.equals(null) 返回 false,e.compareTo(null) 也应该抛出 NullPointerException。

您必须改为创建一个新接口,例如 ComparableNull

另请参阅:

You should not implement Comparable to accept null, as it breaks the interface's contract.

From https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html :

Note that null is not an instance of any class, and e.compareTo(null) should throw a NullPointerException even though e.equals(null) returns false.

You must instead create a new interface, e.g. ComparableNull instead.

See also:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文