使用 Java 集合排序时如何处理空值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3671826/
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
How to handle nulls when using Java collection sort
提问by Giancarlo Corzo
When using Collection.sort in Java what should I return when one of the inner objects is null
在 Java 中使用 Collection.sort 当内部对象之一为空时我应该返回什么
Example:
例子:
Collections.sort(list, new Comparator<MyBean>() {
public int compare(MyBean o1, MyBean o2) {
return o2.getDate().compareTo(o1.getDate());
}
});
Lets say o2 is not null but o2.getDate() it is, so should I return 1 or -1 or 0 when adding a null validation?
可以说 o2 不是 null 但 o2.getDate() 是,那么在添加 null 验证时我应该返回 1 还是 -1 或 0 ?
采纳答案by Nikita Rybak
Naturally, it's your choice. Whatever logic you write, it will define sorting rules. So 'should' isn't really the right word here.
当然,这是你的选择。无论您编写什么逻辑,它都会定义排序规则。所以“应该”在这里并不是真正正确的词。
If you want null to appear before any other element, something like this could do
如果您希望 null 出现在任何其他元素之前,可以这样做
public int compare(MyBean o1, MyBean o2) {
if (o1.getDate() == null) {
return (o2.getDate() == null) ? 0 : -1;
}
if (o2.getDate() == null) {
return 1;
}
return o2.getDate().compareTo(o1.getDate());
}
回答by Colin Hebert
It depends, do you consider null as a big value or a low value.
这取决于,您认为 null 是大值还是小值。
You can consider most of the time that null < everything else, but it depends on the context.
大多数情况下,您可以考虑 null < 其他所有内容,但这取决于上下文。
And 0 would be a terrible return value here.
在这里 0 将是一个可怕的返回值。
回答by Elidio Marquina
Following the answer from Nikita Rybak, i'm already have a enum comparator, and only add the null logic from here.
按照 Nikita Rybak 的回答,我已经有了一个枚举比较器,并且只从这里添加了空逻辑。
public enum Orden implements Comparator<CmunParametrosDTO> {
ByStrDescripcion {
public int compare(CmunParametrosDTO item1, CmunParametrosDTO item2) {
if (item1.getStrDescripcion() == null && item2.getStrDescripcion() == null)
return 0;
if (item1.getStrDescripcion() == null)
return 1;
else if (item2.getStrDescripcion() == null)
return -1;
return item1.getStrDescripcion().compareTo(item2.getStrDescripcion());
}
}
public abstract int compare(CmunParametrosDTO item1, CmunParametrosDTO item2);
public Comparator<CmunParametrosDTO> ascending() {
return this;
}
public Comparator<CmunParametrosDTO> descending() {
return Collections.reverseOrder(this);
}
}
In this form i can call the sort method on my list.
在这种形式中,我可以调用列表中的 sort 方法。
if(isBolOrdAscendente()) Collections.sort(listado, CmunParametrosDTO.Orden.ByStrDescripcion .ascending());
else Collections.sort(listado, CmunParametrosDTO.Orden.ByStrDescripcion .descending());
回答by user1438038
In Java 8 you can also use nullsFirst()
:
在 Java 8 中,您还可以使用nullsFirst()
:
Comparator.nullsFirst(Date::compareTo).compare(dateOne, dateTwo);
Or nullsLast()
:
或者nullsLast()
:
Comparator.nullsLast(Date::compareTo).compare(dateOne, dateTwo);
These methods will either sort null
to the beginning or to the end. There is no wrong or right whether you consider null
bigger or smaller than another objects. This is totally up to you, as others stated already.
这些方法将排序null
到开头或结尾。无论您认为null
比其他物体更大或更小,都没有错或对。正如其他人已经说过的那样,这完全取决于您。
回答by davidddp
Depending on whether the object is null, or the content of the object is null.
取决于对象是否为空,或者对象的内容为空。
The object is null:
对象为空:
import static java.util.Comparator.*;
List<Data> listOfData = Arrays.asList(
new Data("foo"),
null,
new Data("bar"),
new Data("nyu"));
listOfData.sort(nullsFirst(comparing(Data::getValue)));
listOfData.forEach(System.out::println);
//OUTPUT:
// null
// Data(bar)
// Data(foo)
// Data(nyu)
The content of the object is null:
对象的内容为空:
List<Data> listOfData = Arrays.asList(
new Data("foo"),
new Data(null),
new Data("bar"),
new Data("nyu"));
listOfData.sort(nullsFirst(
comparing(Data::getValue, nullsFirst(naturalOrder()))));
listOfData.forEach(System.out::println);
//OUTPUT:
// Data(null)
// Data(bar)
// Data(foo)
// Data(nyu)