英文:
Java Comparator.nullsLast and Comparator.nullsFirst not working as expected
问题
以下是您提供的内容的中文翻译:
我有以下方法,在尝试按列表中为null的字段进行排序时,在sort()
中抛出NPE:
private List<MobileSearch> sortAscAndDesc(final MobilesPostResponse mobilesPostResponse,
final String[] split,
final Comparator<MobileSearch> comparing) {
final var direction = getDirection(split);
final var modifiableList = new ArrayList<MobileSearch>(mobilesPostResponse.getMobiles());
final var comparator = direction.equals(Constants.ASC)
? comparing.thenComparing(Comparator.nullsLast(comparing))
: comparing.thenComparing(Comparator.nullsFirst(comparing.reversed()));
modifiableList.sort(comparator);
return modifiableList;
}
作为参数传递了一个比较器,该比较器使用了几个keyExtractors。
您可以在下面看到一些方法调用:
final var sortedList = sortAscAndDesc(mobilesPostResponse, split, Comparator.comparing(MobileSearch::getMsisdn));
final var sortedList = sortAscAndDesc(mobilesPostResponse, split, Comparator.comparing(MobileSearch::getAssignedProfile));
我想要的只是在每种情况下应用modifiableList.sort()
时避免获取NPE,但是使用我的代码片段无法实现。有什么想法?
英文:
I have the following method, which throws NPE in sort()
when I am trying to sort by a field which is null in the list :
private List<MobileSearch> sortAscAndDesc(final MobilesPostResponse mobilesPostResponse,
final String[] split,
final Comparator<MobileSearch> comparing) {
final var direction = getDirection(split);
final var modifiableList = new ArrayList<MobileSearch>(mobilesPostResponse.getMobiles());
final var comparator = direction.equals(Constants.ASC)
? comparing.thenComparing(Comparator.nullsLast(comparing))
: comparing.thenComparing(Comparator.nullsFirst(comparing.reversed()));
modifiableList.sort(comparator);
return modifiableList;
}
A comparator is passed as a parameter which is using several keyExtractors.
You can see below some method calls :
final var sortedList = sortAscAndDesc(mobilesPostResponse, split, Comparator.comparing(MobileSearch::getMsisdn));
final var sortedList = sortAscAndDesc(mobilesPostResponse, split, Comparator.comparing(MobileSearch::getAssignedProfile));
All I want is to avoid getting NPE when applying the modifiableList.sort()
in EVERY case, but with my piece of code I can not achieve it. Any ideas?
答案1
得分: 2
您正在使用comparing.thenComparing(...)
对空值进行比较,然后才使用Comparators.nulls...(..)
。您只需要使用Comparator.nulls...(..)
,如下所示:
private List<MobileSearch> sortAscAndDesc(final MobilesPostResponse mobilesPostResponse,
final String[] split,
final Comparator<MobileSearch> comparing) {
final var direction = getDirection(split);
final var modifiableList = new ArrayList<MobileSearch>(mobilesPostResponse.getMobiles());
final var comparator = direction.equals(Constants.ASC)
? Comparator.nullsLast(comparing)
: Comparator.nullsFirst(comparing.reversed());
modifiableList.sort(comparator);
return modifiableList;
}
英文:
You are using the comparing
comparator on the null values by using comparing.thenComparing(...)
and then only you use the Comparators.nulls...(..)
. You just need the Comparator.nulls...(..)
like this :
private List<MobileSearch> sortAscAndDesc(final MobilesPostResponse mobilesPostResponse,
final String[] split,
final Comparator<MobileSearch> comparing) {
final var direction = getDirection(split);
final var modifiableList = new ArrayList<MobileSearch>(mobilesPostResponse.getMobiles());
final var comparator = direction.equals(Constants.ASC)
? Comparator.nullsLast(comparing)
: Comparator.nullsFirst(comparing.reversed());
modifiableList.sort(comparator);
return modifiableList;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论