(자바) Collectos.toMap()의 다중 사용
Collectors.toMap이 무엇인가요? 스트림을 사용할 때 마지막 작업으로 collect()를 사용하여 스트림의 요소를 수집하고 특정 데이터 구조로 변환할 수 있습니다. 이 시점에서 Collectors.toMap은 Map으로 변환하는 데 사용됩니다. 일반적으로 사용되는 toMap은 다음과 같습니다. List<String> strings = Arrays.asList(“apple”, “banana”, “pear”); Map<Integer, String> map = strings.stream() .collect(Collectors.toMap(String::length, Function.identity())); System.out.println(map); // 결과: {4=pear, 5=apple, 6=banana} 그러나 위의 코드에는 문제가 있습니다. 중복 … Read more