15. Map 요소 집계

// 모든 값의 합계 계산 int sum = map.values().stream().mapToInt(Integer::intValue).sum(); // 모든 값의 평균 계산 double average = map.values().stream().mapToInt(Integer::intValue).average().orElse(Double.NaN); // 최대값 계산 int max = map.values().stream().mapToInt(Integer::intValue).max().orElse(Integer.MIN_VALUE); // 최소값 계산 int min = map.values().stream().mapToInt(Integer::intValue).min().orElse(Integer.MAX_VALUE);

14. Map 요소 탐색

// 키에 따른 오름차순 탐색 Iterator keyIterator = new TreeMap(map).keySet().iterator(); while (keyIterator.hasNext()) { String key = keyIterator.next(); int value = map.get(key); System.out.println(key + " : " + value); } // 값에 따른 내림차순 탐색 List entryList = new ArrayList(map.entrySet()); entryList.sort(Collections.reverseOrder(Map.Entry.comparingByValue())); Iterator valueIterator = entryList.iterator(); wh..

13. Map 요소 병렬 처리

// 병렬 처리를 위한 맵 생성 Map parallelMap = new ConcurrentHashMap(); parallelMap.put("apple", 1); parallelMap.put("banana", 2); parallelMap.put("cherry", 3); // 모든 요소에 1을 더한 새로운 맵 생성 (병렬 처리) Map parallelMappedMap = parallelMap.entrySet().parallelStream() .collect(Collectors.toConcurrentMap(Map.Entry::getKey, entry -> entry.getValue() + 1));

12. Map 요소 비교

// 두 맵이 동일한지 비교 boolean isEqual = map1.equals(map2);

11. Map 요소 변환

// 값에 1을 더한 새로운 맵 생성 Map mappedMap = map.entrySet().stream() .collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue() + 1));

10. Map 요소 필터링

// 값이 2 이상인 요소만 필터링 Map filteredMap = map.entrySet().stream() .filter(entry -> entry.getValue() >= 2) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));

9. Map 요소 병합

// 두 맵 병합 (동일한 키가 있다면 뒤의 값으로 대체) Map map1 = new HashMap(); map1.put("apple", 1); map1.put("banana", 2); Map map2 = new HashMap(); map2.put("banana", 3); map2.put("cherry", 4); map1.putAll(map2); // map1: {"apple": 1, "banana": 3, "cherry": 4}

8. Map 요소 정렬

// 키로 정렬 Map sortedByKey = new TreeMap(map); // 값으로 정렬 (entrySet()을 리스트로 변환하여 정렬) List list = new ArrayList(map.entrySet()); list.sort(Map.Entry.comparingByValue()); Map sortedByValue = new LinkedHashMap(); for (Map.Entry entry : list) { sortedByValue.put(entry.getKey(), entry.getValue()); }

7. Map 요소 검색

// 키 검색 boolean containsKey = map.containsKey("apple"); // 값 검색 boolean containsValue = map.containsValue(1); // 요소 검색 boolean containsEntry = map.entrySet().contains(Map.entry("apple", 1));

6. Map 키, 값, 요소 추출

// 모든 키 추출 Set keys = map.keySet(); // 모든 값 추출 Collection values = map.values(); // 모든 요소 추출 Set entries = map.entrySet();

5. Map 요소 삭제

// 요소 삭제 map.remove("cherry"); // 모든 요소 삭제 map.clear();

4. Map 요소 추가 및 갱신

// 요소 추가 map.put("orange", 4); // 요소 갱신 map.put("banana", 5);