从对象列表中筛选出仅需要的重复记录,Java(版本低于8)。

huangapple go评论67阅读模式
英文:

Filter only the needed duplicate records from List of Objects java (version less than 8)

问题

我必须从对象列表中筛选出重复项,这相当容易。但是,在筛选重复项时,我有一个内部条件。以下是我的代码。

List<Person> personsList = Arrays.asList(
                    new Person(ID: 1234, dateOfJoin: new Date("2020-08-31 17:32:46"), status: 'Accepted'),
                    new Person(ID: 1234, dateOfJoin: new Date("2020-08-31 17:30:49"), status: 'Rejected'),
                    new Person(ID: 3456, dateOfJoin: new Date("2020-08-29 13:23:34"), status: 'Waiting'));

我必须筛选出 personsList 其中 ID 相等,并且我必须获取最近的 dateOfJoin 的人员。因此,我的最终筛选列表将如下所示。

List<Person> filteredPersonsList = Arrays.asList(
                        new Person(ID: 1234, dateOfJoin: new Date("2020-08-31 17:30:49"), status: 'Rejected'),
                        new Person(ID: 3456, dateOfJoin: new Date("2020-08-29 13:23:34"), status: 'Waiting'));

如何筛选这个列表?请问有什么建议吗?

谢谢。

英文:

I have to filter the duplicates from list of objects which is pretty easy. But, here I have an internal condition while filtering the duplicates. Here is my code.

List&lt;Person&gt; personsList = Arrays.asList(
                new Person(ID: 1234, dateOfJoin: new Date(&quot;2020-08-31 17:32:46&quot;), status: &#39;Accepted&#39;),
                new Person(ID: 1234, dateOfJoin: new Date(&quot;2020-08-31 17:30:49&quot;), status: &#39;Rejected&#39;),
                new Person(ID: 3456, dateOfJoin: new Date(&quot;2020-08-29 13:23:34&quot;), status: &#39;Waiting&#39;));

I have to filter the personsList where ID is equal and I have to get the most recent dateOfJoin person. So, my final filtered list would be like the following.

List&lt;Person&gt; filteredPersonsList = Arrays.asList(
                    new Person(ID: 1234, dateOfJoin: new Date(&quot;2020-08-31 17:30:49&quot;), status: &#39;Rejected&#39;),
                    new Person(ID: 3456, dateOfJoin: new Date(&quot;2020-08-29 13:23:34&quot;), status: &#39;Waiting&#39;));

How could I filter this? Any suggestions please?

Thanks.

答案1

得分: 2

假设存在相应的获取器,看起来像这样。

    Collection<Person> result = personList.stream()
        .collect(Collectors.groupingBy(
                     Person::getId,
                     Collectors.collectingAndThen(
                         Collectors.maxBy(Comparator.comparing(Person::getDateOfJoin)),
                         Optional::get))
        .values();

不幸的是,maxBy 返回 Optional,需要进一步解包。

英文:

Assuming there are corresponding getters look like this.

    Collection&lt;Person&gt; result = personList.stream()
        .collect(Collectors.groupingBy(
                     Person::getId,
                     Collectors.collectingAndThen(
                         Collectors.maxBy(Comparator.comparing(Person::getDateOfJoin),
                         Optional::get)))
        .values();

Unfortunately, maxBy returns Optional and needs further unwrapping

答案2

得分: 2

你可以使用 toMapmaxBy 来获取具有相同 id 的最大 dateOfJoin,并将映射的值放入一个 ArrayList 中。

List<Person> filteredPersonsList = new ArrayList<>(personsList
    .stream()
    .collect(Collectors.toMap(Person::getID, Function.identity(),
        BinaryOperator.maxBy(Comparator.comparing(Person::getDateOfJoin))))
    .values());
英文:

You can use toMap and maxBy to get max dateOfJoin with same id and take values of the map in an ArrayList.

List&lt;Person&gt; filteredPersonsList = 
        new ArrayList&lt;&gt;(personsList
              .stream()
              .collect(Collectors.toMap(Person::getID, Function.identity(),
                    BinaryOperator.maxBy(Comparator.comparing(Person::getDateOfJoin))))
              .values());

huangapple
  • 本文由 发表于 2020年9月2日 04:48:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/63695248.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定