简体   繁体   English

具有一对多关系的比较器

[英]comparator with one to many relation filed

i have a DTO object ClientInfo that holds a List of document entity.我有一个包含文档实体列表的 DTO 对象 ClientInfo。

i use the following compare method to sort clientInfo list by documentCreationDate DESC.我使用以下比较方法按 documentCreationDate DESC 对 clientInfo 列表进行排序。

    @Override
        public int compareTo(ClientInfo o) {

            Date firstmaxDate = this.getDocuments().stream().map(d -> d.getCreateDate()).max(Date::compareTo).get();
            Date secondmaxDate = o.getDocuments().stream().map(d -> d.getCreateDate()).max(Date::compareTo).get();
            return firstmaxDate.compareTo(secondmaxDate);

        }
List<ClientInfo> clientInfos= serverReturnedList......;
Collections.sort(clientInfos);

But its getting an exception telling that comparison rule has been violated.但它得到一个例外,告诉比较规则已被违反。 so im not getting the expected result.所以我没有得到预期的结果。 can anyone explain whats the way to achieve this.谁能解释一下实现这一目标的方法。

This is the exception stack trace这是异常堆栈跟踪

java.util.NoSuchElementException: No value present
        at java.util.Optional.get(Optional.java:135)
        at com.orsbv.hcs.dto.ClientInfo.compareTo(ClientInfo.java:346)
        at com.orsbv.hcs.dto.ClientInfo.compareTo(ClientInfo.java:24)
        at java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:320)
        at java.util.ComparableTimSort.sort(ComparableTimSort.java:188)
        at java.util.Arrays.sort(Arrays.java:1312)
        at java.util.Arrays.sort(Arrays.java:1506)
        at java.util.ArrayList.sort(ArrayList.java:1462)
        at java.util.Collections.sort(Collections.java:141)

You should keep your compareTo() method as it's written, because it's sorting according to natural ordering (ascending), which makes sense.您应该保留compareTo()方法,因为它按照自然顺序(升序)进行排序,这是有道理的。

Instead you should change the sort, to tell it to sort in reverse (descending) order.相反,您应该更改排序,以告诉它以反向(降序)顺序排序。

List<ClientInfo> clientInfoList = ...
Collections.sort(clientInfoList, Comparator.reverseOrder());

UPDATE based on info added to question:根据添加到问题的信息更新:

Some of your ClientInfo don't have any documents that you can use to check created date.您的某些ClientInfo没有任何可用于检查创建日期的文档。

When you do this:当你这样做时:

 Date firstmaxDate = this.getDocuments().stream().map(d -> d.getCreateDate()).max(Date::compareTo)

the max() returns an Optional<Date> which allows it to return Optional.empty() in the case of an empty stream. max()返回一个Optional<Date>允许它在空流的情况下返回Optional.empty() When you've a ClientInfo without any document, you're calling get() on an empty Optional , which causes this exception.当您有一个没有任何文档的 ClientInfo 时,您会在空的Optional上调用get() ,这会导致此异常。

You need to determine whether this case can occur in the real data, and if so, how you want to sort ClientInfo with no document.您需要确定在真实数据中是否会出现这种情况,如果是,您希望如何对没有文档的ClientInfo 进行排序。 If all your createdDates are in the past, you could simply replace "no created date" with "now":如果你所有的 createdDates 都是过去的,你可以简单地用“now”替换“no created date”:

        Date thisDate = this.getDocuments()
                .stream()
                .map(Document::getCreateDate)
                .max(Date::compareTo)
                .orElseGet(Date::new);
        Date otherDate = o.getDocuments()
                .stream()
                .map(Document::getCreateDate)
                .max(Date::compareTo)
                .orElseGet(Date::new);

Or, you could stop before get() , work with Optional<Date> and check for isPresent() and handle as needed.或者,您可以在get()之前停止,使用Optional<Date>并检查isPresent()并根据需要进行处理。

firstly i added a maxDocuemntCreationDate field to clientInfo.首先,我向 clientInfo 添加了一个 maxDocuemntCreationDate 字段。

  Date maxCreationDate = clientInfo.getDocuments()
                        .stream()
                        .map(Document :: getCreateDate)
                        .max(Date::compareTo)
                        .orElseGet(Date::new);

Then i compare in this fashion然后我以这种方式比较

 @Override
            public int compareTo(ClientInfo o) {


              return 
              this.getMaxCreationDate().compareTo(o.getMaxCreationDate());

            }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM