Backend/Java
Map에 stream 사용하기
findmypiece
2021. 6. 18. 13:46
728x90
Map<Integer, Integer> map = new HashMap();
for (int i = 0; i < 10; i++) {
map.put(i, i);
}
Map<Integer, Integer> toMap = map.entrySet().stream()
.filter(m -> m.getKey() < 5)
.collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue()));
System.out.println(toMap);
728x90