Home > other >  How to Group a List of objects by Two fields and obtain a List of objects of resulting type
How to Group a List of objects by Two fields and obtain a List of objects of resulting type

Time:04-12

I have a list of objects shown below:

public class AssignDTO {    
    private String assignmentIbanId;    
    private String agreementFileId;    
    private BankData bankData;

   // getter and setter
}

[
   {
      "assignmentIbanId":"6146dd87-2344-4149-9396-8e8a88493dd6",
      "agreementFileId":"36de8604-ae56-49b3-b972-8071b459bf82",
      "bankData": { "useType" : "a" }
   },
   {
      "assignmentIbanId":"44cb8cc6-fe28-4e44-a78c-48a908795f38",
      "agreementFileId":"854f0c37-b9d8-4533-9c02-0c1b64a909bd",
      "bankData": { "useType" : "b" }
   },
   {
      "assignmentIbanId":"44cb8cc6-fe28-4e44-a78c-48a908795f38",
      "agreementFileId":"854f0c37-b9d8-4533-9c02-0c1b64a909bd",
      "bankData": { "useType" : "c" }
   }
]

How can I group the list by agreementFileId and agreementFileId fields like below

public class ResultDTO {    
    private String assignmentIbanId;    
    private String agreementFileId;    
    private Set<BankData> bankAccounts;

   // getter and setter
}

[
   {
      "assignmentIbanId":"6146dd87-2344-4149-9396-8e8a88493dd6",
      "agreementFileId":"36de8604-ae56-49b3-b972-8071b459bf82",
      "bankAccounts": [{ "useType" : "a" }]
   }
   {
      "assignmentIbanId":"44cb8cc6-fe28-4e44-a78c-48a908795f38",
      "agreementFileId":"854f0c37-b9d8-4533-9c02-0c1b64a909bd",
      "bankAccounts": [{ "useType" : "b" }, { "useType" : "c" }]
   }
]

CodePudding user response:

It would help to know the required return type. Here are two possible solutions:

Map<String, Map<String, List<AssignDTO>>> collect = list.stream().collect(
    Collectors.groupingBy(AssignIbanEntityDTO::getAgreementFileId,
    Collectors.groupingBy(AssignDTO::getBankData)));

Structure:

{ "a" -> { "b" -> [obj1, obj2] } }

Another option would be:

Map<List<String>, List<AssignDTO>> collect = list.stream().collect(
    Collectors.groupingBy(dto -> List.of(dto.getAgreementFileId(), dto.getBankData()));

Structure:

{ ["a", "b"] -> [obj1, obj2] }

Without additional information it's hard to tell which to choose.

CodePudding user response:

As the first step, you might either create a nested map applying Collector.groupingBy() twice, or group the data using an object that carry two values, like a Map.Entry, as key.

In both cases as a downstream collector a combination of mapping() and toSet() would be handy:

Collectors.mapping(AssignDTO::getBankData, Collectors.toSet()

As a result, will get an intermediate map having Set<BankData> as its values.

Then create a stream over the entry set and apply map() to turn each entry into a ResultDTO and collect the result into a list either with toList() (Java 16 ) or by utilizing Collectors.toList().

That's how it might look like (example with map entry):

public static void main(String[] args) {
    List<AssignDTO> source =
        List.of(new AssignDTO("6146dd87-2344-4149-9396-8e8a88493dd6",
                    "36de8604-ae56-49b3-b972-8071b459bf82", new BankData("a")),
                new AssignDTO("44cb8cc6-fe28-4e44-a78c-48a908795f38",
                    "854f0c37-b9d8-4533-9c02-0c1b64a909bd", new BankData("b")),
                new AssignDTO("44cb8cc6-fe28-4e44-a78c-48a908795f38",
                    "854f0c37-b9d8-4533-9c02-0c1b64a909bd", new BankData("c")));
    
    List<ResultDTO> result =
        source.stream()
            .collect(Collectors.groupingBy(assignDTO -> Map.entry(assignDTO.getAgreementFileId(), assignDTO.getAgreementFileId()),
                        Collectors.mapping(AssignDTO::getBankData, Collectors.toSet()))) // creating an intermediate map `Map<Map.Entry<String, String>>, Set<BankData>>
            .entrySet().stream()
            .map(entry -> new ResultDTO(entry.getKey().getKey(),
                                        entry.getKey().getValue(),
                                        entry.getValue()))
            .collect(Collectors.toList());

    result.forEach(System.out::println);
}

Output

ResultDTO{
    assignmentIbanId : '854f0c37-b9d8-4533-9c02-0c1b64a909bd',
    agreementFileId : '854f0c37-b9d8-4533-9c02-0c1b64a909bd',
    bankAccounts : [{useType : 'b'}, {useType : 'c'}]}
ResultDTO{
    assignmentIbanId : '36de8604-ae56-49b3-b972-8071b459bf82',
    agreementFileId : '36de8604-ae56-49b3-b972-8071b459bf82',
    bankAccounts : [{useType : 'a'}]}
  • Related