Java63 15. Map 요소 집계 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); 2023. 5. 3. 14. Map 요소 탐색 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.. 2023. 5. 3. 13. Map 요소 병렬 처리 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)); 2023. 5. 3. 12. Map 요소 비교 12. Map 요소 비교 // 두 맵이 동일한지 비교 boolean isEqual = map1.equals(map2); 2023. 5. 3. 11. Map 요소 변환 11. Map 요소 변환 // 값에 1을 더한 새로운 맵 생성 Map mappedMap = map.entrySet().stream() .collect(Collectors.toMap(Map.Entry::getKey, entry -> entry.getValue() + 1)); 2023. 5. 3. 10. Map 요소 필터링 10. Map 요소 필터링 // 값이 2 이상인 요소만 필터링 Map filteredMap = map.entrySet().stream() .filter(entry -> entry.getValue() >= 2) .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)); 2023. 5. 3. 이전 1 ··· 4 5 6 7 8 9 10 11 다음