1

I have an Arraylist like below where I get some values from database due to JOIN

Structure I am getting now

  List<Account> accountList= new ArrayList<>();
    accountList.add(new Account("123","ram","2023-05-09T13:30:00.000+00:00"));
    accountList.add(new Account("123", "ram","2023-05-09T14:30:00.000+00:00"));

Desired Output after converting

   accountList.add(new Account("123","ram","2023-05-09T13:30:00.000+00:00"));

My Pojo class Account.java

String id;

String name;

Date startDate;

I have to use streams for making this possible to get List of unique Account Objects based on id

  • 1
    there should be a lot of source material online, but the basic should be to override equals/hashcode, then use distinct() on the stream and collect to a list. – experiment unit 1998X May 09 '23 at 15:07
  • Does this answer your question? [Java8 Streams - Remove Duplicates With Stream Distinct](https://stackoverflow.com/questions/27911406/java8-streams-remove-duplicates-with-stream-distinct) – experiment unit 1998X May 09 '23 at 15:08
  • So you want to consolidate similar items to earlier of the two date-times? You need to get *specific* on what you want. – Basil Bourque May 09 '23 at 21:30
  • By the way, both of the `Date` classes were years ago supplanted by the modern *java.time* classes defined in JSR 310. Use a type of `OffsetDateTime` for your input strings. – Basil Bourque May 09 '23 at 21:36

2 Answers2

2

You can get a list of unique items based on their ID using streams and the Collectors.toMap() method, as follows:

List<Account> accountList = new ArrayList<>();
accountList.add(new Account("123","ram","2023-05-09T13:30:00.000+00:00"));
accountList.add(new Account("123", "ram","2023-05-09T14:30:00.000+00:00"));

List<Account> uniqueAccounts = 
    accountList
        .stream()
        .collect(
            Collectors.toMap(
                Account::getId, 
                Function.identity(),
                (account1, account2) -> account1
            )
        )
        .values()
        .stream()
        .collect(Collectors.toList());

The Function.identity() method is used as the value mapper to simply return the Account object as the value. In case of duplicate keys, we use the merge function (account1, account2) -> account1 to keep the first occurrence of the Account object.

Note that in the above code, we are keeping the first occurrence of the Account object in case of duplicates. You can modify the merge function to keep the last occurrence or use more complex logic to merge the duplicate objects as needed.

The import package for the Function class is java.util.function.

Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
0

You can convert it to set and generate equalsAndHashcode based on id of pojo. I think it should work.

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 09 '23 at 19:06