不能用比较器对 null 进行排序
Cant sort null with comparator
当我尝试对某个用户的个人资料中的空值进行排序时,我总是收到 nullpointerexception
。我的印象是 Google Collection
会处理这些空值,但它似乎不起作用。
这是我使用的代码:
Comparator<UserModel> firstName_comparator = new Comparator<UserModel>() {
@Override
public int compare(UserModel c1, UserModel c2) {
return c1.getProfile().getFirstName().toLowerCase()
.compareTo(c2.getProfile().getFirstName().toLowerCase());
}
};
Collections.sort(users, Ordering.from(firstName_comparator).nullsLast());
此特定行抛出 nullpointerexception
:
.compareTo(c2.getProfile().getFirstName().toLowerCase());
因为 getProfile()
为空。
我该如何解决这个问题?我希望能够使用空值对我的用户进行排序。
不,Guava 不会忽略您的 NullPointerException。您正在提供一个比较器,这个比较器应该遵守比较器合同。抛出 NullPointerException 不是约定的一部分。
String firstName1 = c1.getProfile() == null? null : c1.getProfile().getFirstName().toLowerCase();
String firstName2 = c2.getProfile() == null? null : c1.getProfile().getFirstName().toLowerCase();
return Ordering.natural().nullsFirst().compare(firstName1, firstName2);
// or nullsLast(), depending on what you prefer
或者,更简单:
Comparator<UserModel> comparator =
Ordering.natural()
.nullsFirst()
.onResultOf(model -> c1.getProfile() == null? null : c1.getProfile().getFirstName().toLowerCase());
I was under the impression Google Collection would handle these null
values
nullLast 方法只会检查集合中的特定元素是否为 null
并将其放在集合的末尾。
This specific line throws the nullpointerexception:
.compareTo(c2.getProfile().getFirstName().toLowerCase());
这里有两种可能的空值:
c2.getProfile()
为空
c2.getProfile().getFirstName()
为空
您需要明确保护您的 Comparator
实施免受这些 null
的影响。
当我尝试对某个用户的个人资料中的空值进行排序时,我总是收到 nullpointerexception
。我的印象是 Google Collection
会处理这些空值,但它似乎不起作用。
这是我使用的代码:
Comparator<UserModel> firstName_comparator = new Comparator<UserModel>() {
@Override
public int compare(UserModel c1, UserModel c2) {
return c1.getProfile().getFirstName().toLowerCase()
.compareTo(c2.getProfile().getFirstName().toLowerCase());
}
};
Collections.sort(users, Ordering.from(firstName_comparator).nullsLast());
此特定行抛出 nullpointerexception
:
.compareTo(c2.getProfile().getFirstName().toLowerCase());
因为 getProfile()
为空。
我该如何解决这个问题?我希望能够使用空值对我的用户进行排序。
不,Guava 不会忽略您的 NullPointerException。您正在提供一个比较器,这个比较器应该遵守比较器合同。抛出 NullPointerException 不是约定的一部分。
String firstName1 = c1.getProfile() == null? null : c1.getProfile().getFirstName().toLowerCase();
String firstName2 = c2.getProfile() == null? null : c1.getProfile().getFirstName().toLowerCase();
return Ordering.natural().nullsFirst().compare(firstName1, firstName2);
// or nullsLast(), depending on what you prefer
或者,更简单:
Comparator<UserModel> comparator =
Ordering.natural()
.nullsFirst()
.onResultOf(model -> c1.getProfile() == null? null : c1.getProfile().getFirstName().toLowerCase());
I was under the impression Google Collection would handle these null values
nullLast 方法只会检查集合中的特定元素是否为 null
并将其放在集合的末尾。
This specific line throws the nullpointerexception:
.compareTo(c2.getProfile().getFirstName().toLowerCase());
这里有两种可能的空值:
c2.getProfile()
为空c2.getProfile().getFirstName()
为空
您需要明确保护您的 Comparator
实施免受这些 null
的影响。