Java출력 결과보통 · 20점#컬렉션#문자열#반복문

84. TreeMap으로 단어 개수 세기

다음 Java 프로그램의 실행 결과를 쓰시오.

1import java.util.Map;
2import java.util.TreeMap;
3
4public class Main {
5 public static void main(String[] args) {
6 String[] words = {"kiwi", "fig", "apple", "pear", "fig", "plum"};
7 Map<String, Integer> count = new TreeMap<>();
8 TreeMap<Integer, String> byLen = new TreeMap<>();
9 for (String w : words) {
10 count.put(w, count.getOrDefault(w, 0) + 1);
11 byLen.put(w.length(), w);
12 }
13 System.out.println(count);
14 System.out.println(byLen);
15 System.out.println(byLen.lastKey() + count.get("fig"));
16 }
17}

해설을 먼저 보면 이후 정답 시 점수가 50%만 반영됩니다.