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

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

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

问题

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

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

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

  1. List<Person> filteredPersonsList = Arrays.asList(
  2. new Person(ID: 1234, dateOfJoin: new Date("2020-08-31 17:30:49"), status: 'Rejected'),
  3. 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.

  1. List&lt;Person&gt; personsList = Arrays.asList(
  2. new Person(ID: 1234, dateOfJoin: new Date(&quot;2020-08-31 17:32:46&quot;), status: &#39;Accepted&#39;),
  3. new Person(ID: 1234, dateOfJoin: new Date(&quot;2020-08-31 17:30:49&quot;), status: &#39;Rejected&#39;),
  4. 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.

  1. List&lt;Person&gt; filteredPersonsList = Arrays.asList(
  2. new Person(ID: 1234, dateOfJoin: new Date(&quot;2020-08-31 17:30:49&quot;), status: &#39;Rejected&#39;),
  3. 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

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

  1. Collection<Person> result = personList.stream()
  2. .collect(Collectors.groupingBy(
  3. Person::getId,
  4. Collectors.collectingAndThen(
  5. Collectors.maxBy(Comparator.comparing(Person::getDateOfJoin)),
  6. Optional::get))
  7. .values();

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

英文:

Assuming there are corresponding getters look like this.

  1. Collection&lt;Person&gt; result = personList.stream()
  2. .collect(Collectors.groupingBy(
  3. Person::getId,
  4. Collectors.collectingAndThen(
  5. Collectors.maxBy(Comparator.comparing(Person::getDateOfJoin),
  6. Optional::get)))
  7. .values();

Unfortunately, maxBy returns Optional and needs further unwrapping

答案2

得分: 2

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

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

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

  1. List&lt;Person&gt; filteredPersonsList =
  2. new ArrayList&lt;&gt;(personsList
  3. .stream()
  4. .collect(Collectors.toMap(Person::getID, Function.identity(),
  5. BinaryOperator.maxBy(Comparator.comparing(Person::getDateOfJoin))))
  6. .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:

确定