//对map集合分组List<Map<String, Object>> list = new ArrayList<>();for (int i = 0; i < 20; i++) {Map<String, Object> map = new HashMap<>();map.put("key", (int) (Math.random() * 10));map.put("value", i);list.add(map);}Map<String, List<Map<String, Object>>> collect = list.stream().collect(Collectors.groupingBy(e -> {return e.get("key").toString();}));Iterator<Map.Entry<String, List<Map<String, Object>>>> iterator = collect.entrySet().iterator();while (iterator.hasNext()) {Map.Entry<String, List<Map<String, Object>>> next = iterator.next();List<Map<String, Object>> value = next.getValue();System.out.println("-----------------");for (Map<String, Object> stringObjectMap : value) {System.out.println(stringObjectMap);}}//对实体类进行分组List<SiteEntity> siteEntityList = new ArrayList<>();for (int i = 0; i < 5; i++) {SiteEntity entity = new SiteEntity();entity.setId(i);entity.setName("a" + i);siteEntityList.add(entity);}for (int i = 0; i < 5; i++) {SiteEntity entity = new SiteEntity();entity.setId(i);entity.setName("b" + i);siteEntityList.add(entity);}for (int i = 0; i < 5; i++) {SiteEntity entity = new SiteEntity();entity.setId(i);entity.setName("c" + i);siteEntityList.add(entity);}Map<Integer, List<SiteEntity>> collect1 = siteEntityList.stream().collect(Collectors.groupingBy(SiteEntity::getId));Iterator<Map.Entry<Integer, List<SiteEntity>>> iterator1 = collect1.entrySet().iterator();while (iterator1.hasNext()) {Map.Entry<Integer, List<SiteEntity>> next = iterator1.next();List<SiteEntity> value = next.getValue();System.out.println("-------");value.forEach(e -> System.out.println(e));}//对实体类中的属性进行分组Map<Integer, List<String>> collect2 = siteEntityList.stream().collect(Collectors.groupingBy(SiteEntity::getId, Collectors.mapping(SiteEntity::getName, Collectors.toList())));Iterator<Map.Entry<Integer, List<String>>> iterator2 = collect2.entrySet().iterator();while (iterator2.hasNext()) {Map.Entry<Integer, List<String>> next = iterator2.next();List<String> value = next.getValue();System.out.println("-----------");value.stream().forEach(System.out::println);}}
打印结果
-----------------
{value=6, key=0}
{value=8, key=0}
-----------------
{value=0, key=1}
{value=3, key=1}
{value=5, key=1}
{value=9, key=1}
-----------------
{value=7, key=2}
{value=10, key=2}
{value=17, key=2}
-----------------
{value=2, key=3}
{value=4, key=3}
{value=12, key=3}
{value=15, key=3}
-----------------
{value=18, key=4}
{value=19, key=4}
-----------------
{value=14, key=7}
-----------------
{value=1, key=8}
{value=11, key=8}
{value=13, key=8}
{value=16, key=8}
-------
SiteEntity{id=0, name='a0'}
SiteEntity{id=0, name='b0'}
SiteEntity{id=0, name='c0'}
-------
SiteEntity{id=1, name='a1'}
SiteEntity{id=1, name='b1'}
SiteEntity{id=1, name='c1'}
-------
SiteEntity{id=2, name='a2'}
SiteEntity{id=2, name='b2'}
SiteEntity{id=2, name='c2'}
-------
SiteEntity{id=3, name='a3'}
SiteEntity{id=3, name='b3'}
SiteEntity{id=3, name='c3'}
-------
SiteEntity{id=4, name='a4'}
SiteEntity{id=4, name='b4'}
SiteEntity{id=4, name='c4'}
-----------
a0
b0
c0
-----------
a1
b1
c1
-----------
a2
b2
c2
-----------
a3
b3
c3
-----------
a4
b4
c4Process finished with exit code 0