意外的排序列表

问候,


我有 2 个对象:


请愿

签字人(签字人)

我写了这段代码:


public List<Petition> getTheMostSigned(long groupId){


    List<Petition> petitionList = petitionPersistence.findByStatusAndGroupId(0,groupId);


    _log.info("list avant getTheMostSigned size  : "+petitionList.stream().map(petition -> petition.getSignataires().size()).collect(Collectors.toList()));


    List<Petition> resultList = petitionList.stream()

            .sorted(Comparator.comparingInt(petition -> petition.getSignataires().size()))

            .sorted(Collections.reverseOrder())

            .collect(Collectors.toList());


    _log.info("list apres getTheMostSigned size  : "+resultList.stream().map(petition -> petition.getSignataires().size()).collect(Collectors.toList()));


    return resultList;

getSignaaires() 返回一个列表。


但结果并不是我所期望的:

http://img.mukewang.com/6141a1ca0001c50812430057.jpg

2018-09-12 12:44:25.686 INFO  [http-nio-8080-exec-10][PetitionLocalServiceImpl:390] list avant getTheMostSigned size  : [0, 0, 400, 0, 3, 401, 5501]

2018-09-12 12:44:25.856 INFO  [http-nio-8080-exec-10][PetitionLocalServiceImpl:396] list apres getTheMostSigned size  : [5501, 401, 3, 0, 0, **400**, 0]

如您所见,倒数第二个不是好的。你知道为什么比较器不做这项工作吗?


慕姐8265434
浏览 156回答 3
3回答

慕莱坞森

当您链接两种类型时,结果是预期的。第一个 ( .sorted(Comparator.comparingInt(petition -> petition.getSignataires().size())) 按列表字段大小排序)。然后第二个 ( .sorted(Collections.reverseOrder())) 将第一个排序结果覆盖为最后一个按照 的逆自然顺序排序Petition。当您两次调用排序流操作时,大致就像您使用了这个逻辑:List<Petition> petitionList = ...;// first sortpetitionList.sort(Comparator.comparingInt(petition -> petition.getSignataires().size());// second sortpetitionList.sort(Collections.reversed());您需要的是定义一个Comparator结合这些约束的实例。从 Java 8 开始,您可以创建Comparators 并将它们组合起来,这主要归功于.thenComparingXXX()和.reversed()方法。所以你可以这样做:.sorted(Comparator.comparingInt(petition -> petition.getSignataires().size())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .reversed()&nbsp; &nbsp; &nbsp; &nbsp;)

一只名叫tom的猫

您不需要两次sorted操作。它们不会被组合以产生结果Comparator.第一个构造一个Comparator<Integer>forint size()值,而第二个则忽略前一个调用并应用它自己的Comparator<Petition>( Comparator.<Petition>reverseOrder())。Comparator<Petition> reversedSignaturesSizeComparator&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; = Comparator.<Petition>comparingInt(p -> p.getSignataires().size()).reversed();List<Petition> resultList = petitionList.stream()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .sorted(reversedSignaturesSizeComparator)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());

HUWWW

下面是使用 Collection 的其他重载方法的另一种方法,该方法使用自定义压缩器Comparator<Petition> cmp =&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(Petition left, Petition right) ->&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; left.getSignataires().size() - right.getSignataires().size();List<Petition> resultList = petitionList.stream()&nbsp; &nbsp; &nbsp; &nbsp; .sorted(Collections.reverseOrder(cmp))&nbsp; &nbsp; &nbsp; &nbsp; .collect(Collectors.toList());
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java