JAVA - Stream API List Map 을 원하는 값으로 groupingBy 하기
2021. 6. 3. 14:00ㆍprogramming/java
728x90
companyNo를 기준으로 groupingBy
List<HashMap<String, Object>> stringList = new ArrayList<>();
stringList.add(new HashMap<String, Object>(){{
put("companyNo", 1);
put("email", "aaa@gmail.com");
}});
stringList.add(new HashMap<String, Object>(){{
put("companyNo", 3);
put("email", "bbb@gmail.com");
}});
stringList.add(new HashMap<String, Object>(){{
put("companyNo", 5);
put("email", "ccc@gmail.com");
}});
stringList.add(new HashMap<String, Object>(){{
put("companyNo", 1);
put("email", "ddd@gmail.com");
}});
>> 데이터 형태
[{companyNo=1, email=aaa@gmail.com}, {companyNo=3, email=bbb@gmail.com}, {companyNo=5, email=ccc@gmail.com}, {companyNo=1, email=ddd@gmail.com}]
Map<Integer, List<HashMap<String, Object>>> s = stringList.stream().collect(Collectors.groupingBy(m -> Integer.parseInt(m.get("companyNo").toString()), HashMap::new, Collectors.toList()));
>> 결과
{1=[{companyNo=1, email=aaa@gmail.com}, {companyNo=1, email=ddd@gmail.com}], 3=[{companyNo=3, email=bbb@gmail.com}], 5=[{companyNo=5, email=ccc@gmail.com}]}
HashMap<Integer, List<String>> userList = new HashMap<>();
map.entrySet().stream().forEach(m -> userList.put(m.getKey(), m.getValue().stream().map(s -> s.get("email").toString()).collect(Collectors.toList())));
>> 결과
{1=[aaa@gmail.com, ddd@gmail.com], 3=[bbb@gmail.com], 5=[ccc@gmail.com]}
728x90
'programming > java' 카테고리의 다른 글
Java Garbage Collector (0) | 2021.09.01 |
---|---|
Thread.currentThread() (0) | 2021.01.18 |
[JAVA][Eclipse] 한번에 환경설정하기! (0) | 2015.08.28 |
[JAVA] 한번에 환경설정하기!!! (4) | 2015.08.28 |
[JAVA] throw와 throws 의 차이점 (1) | 2011.12.09 |