How to Iterate through nested Map<Integer, List<abc>>

user5982569 :

How to Iterate through nested Map<Integer, List<abc>>

Class Abc {
    int id ;
    String name;
    Date startDate;
    int rowNum;
}

List<Abc> dupList = list
                    .stream()
                    .collect(Collectors.groupingBy(Abc::getRowNum))
                    .values()
                    .stream()
                    .filter(v-> v.size()>=2)
                    .flatMap(List::stream)
                    .collect(Collectors.toList());
Map<Integer, List<Abc>> byRows = dupList
                                 .stream()
                              .collect(Collectors.groupingBy(Abc::getRowNum));

Map contains fallowing data
Key             value(abc List)
rowNum: 1       rowNum: 1
                startDate: 01/01/2019
                endDate:12/31/2099
                name: Test1
                Id:101
rowNum: 1       rowNum: 1
                startDate: 01/01/2020
                endDate:12/31/2099
                name: Test1
                Id:111
rowNum: 3       rowNum: 3
                startDate: 01/01/2020
                endDate:12/31/2099
                name: Test4
                Id:342
rowNum: 3       rowNum: 3
                startDate: 01/01/2020
                endDate:12/31/2099
                name: Test4
                Id:348

Now I want to print values for key and corresponding List of class Abc.

When I am trying to print the map

Map<Integer, List<Abc>> collect = byRows
                                   .entrySet()
                                   .stream()
                                  .collect(Collectors.toMap(Map.Entry::getKey, 
                                                        Map.Entry::getValue));
System.out.println(collect);

I am getting output as {1=[Abc@1d72673f, Abc@1cffb7e0], 3=[Abc@522530e9, Abc@58dc69f2]} I am not getting actual values from nested loop from ArrayList of class Abc.

expected output:

I want to print startDate for each all rows 
Ex: rowNum  startDate 
    1       01/01/2019 {startDate}
            01/01/2020

    3       startDate: 01/01/2020
            startDate: 01/01/2020
Deadpool :

Basically you are grouping by rowNum and filtering if list has 2 elements. so you can do this all in one step. No need to group by twice

Map<Integer, List<Term>> collect = list
                .stream()
                .collect(Collectors.groupingBy(Abc::getRowNum))
                .entrySet()
                .stream()
                .filter(v-> v.getValue().size()>=2)
                .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

Then if you want to print each element from Map then use forEach, still you might need formatting the data based on your requirement

collect.forEach((k,v)-> {
             System.out.println("RowNum is :"+k);
             v.forEach(o->System.out.println(o.getStartDate()));
           });

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=154877&siteId=1