简体   繁体   中英

Sorting a List inside a complex map

I need help, i could not find a way to resolve this task..

I created this map from an ArrayList in following way:

Map<RamiBean, Map<String, Map<String, List<AllertaBean>>>> complexMap = allerte.stream()
    .distinct()
    .collect(Collectors.groupingBy(
             ab -> ab.getRamoBean(), 
             Collectors.groupingBy(ab -> ab.getPuntoBean().getNomePunto(), 
             Collectors.groupingBy(AllertaBean::getDescAllerta))));

My problem is that i need to group the second level for ab.getPuntoBean().getNomePunto() but i I would like to have this group ordered by another field/method: ab.getPuntoBean().getKm()

try this

Map<RamiBean, Map<String, Map<String, List<AllertaBean>>>> complexMap = allerte.stream()
    .distinct()
    .collect(Collectors.groupingBy(
         ab -> ab.getRamoBean(), 
         Collectors.groupingBy(ab -> ab.getPuntoBean().getNomePunto(), 
         Collectors.toMap(
                 AllertaBean::getDescAllerta,
                 ab -> new ArrayList(Arrays.asList(ab)),
                 (abList1, abList2) -> {
                       abList1.addAll(abList2);
                       abList1.sort(Comparator.comparing(ab -> ab.getPuntoBean().getKm()))
                       return abList1;
                  }
         )
         )
    ));

or this one

Map<RamiBean, Map<String, Map<String, List<AllertaBean>>>> complexMap = allerte.stream()
    .distinct()
    .collect(Collectors.groupingBy(
         ab -> ab.getRamoBean(), 
         Collectors.groupingBy(ab -> ab.getPuntoBean().getNomePunto(), 
         Collectors.groupingBy(AllertaBean::getDescAllerta,
              Collectors. collectingAndThen(Collectors.toList(),
                   abList -> abList.sort(Comparator.comparing(ab -> ab.getPuntoBean().getKm()))
  ))));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM