33import static java .util .stream .Collectors .groupingBy ;
44import static java .util .stream .Collectors .toSet ;
55
6- // Using an EnumMap to associate data with an enum (Pages 171-3 )
6+ // EnumMap을 사용해 열거 타입에 데이터를 연관시키기 (226-228쪽 )
77
8- // Simplistic class representing a plant (Page 171 )
8+ // 식물을 아주 단순하게 표현한 클래스 (226쪽 )
99class Plant {
1010 enum LifeCycle { ANNUAL , PERENNIAL , BIENNIAL }
1111
@@ -23,28 +23,28 @@ enum LifeCycle { ANNUAL, PERENNIAL, BIENNIAL }
2323
2424 public static void main (String [] args ) {
2525 Plant [] garden = {
26- new Plant ("Basil " , LifeCycle .ANNUAL ),
27- new Plant ("Carroway " , LifeCycle .BIENNIAL ),
28- new Plant ("Dill" , LifeCycle .ANNUAL ),
29- new Plant ("Lavendar" , LifeCycle .PERENNIAL ),
30- new Plant ("Parsley" , LifeCycle .BIENNIAL ),
31- new Plant ("Rosemary " , LifeCycle .PERENNIAL )
26+ new Plant ("바질 " , LifeCycle .ANNUAL ),
27+ new Plant ("캐러웨이 " , LifeCycle .BIENNIAL ),
28+ new Plant ("딜" , LifeCycle .ANNUAL ),
29+ new Plant ("라벤더" , LifeCycle .PERENNIAL ),
30+ new Plant ("파슬리" , LifeCycle .BIENNIAL ),
31+ new Plant ("로즈마리 " , LifeCycle .PERENNIAL )
3232 };
3333
34- // Using ordinal() to index into an array - DON'T DO THIS! (Page 171 )
34+ // 코드 37-1 ordinal()을 배열 인덱스로 사용 - 따라 하지 말 것! (226쪽 )
3535 Set <Plant >[] plantsByLifeCycleArr =
3636 (Set <Plant >[]) new Set [Plant .LifeCycle .values ().length ];
3737 for (int i = 0 ; i < plantsByLifeCycleArr .length ; i ++)
3838 plantsByLifeCycleArr [i ] = new HashSet <>();
3939 for (Plant p : garden )
4040 plantsByLifeCycleArr [p .lifeCycle .ordinal ()].add (p );
41- // Print the results
41+ // 결과 출력
4242 for (int i = 0 ; i < plantsByLifeCycleArr .length ; i ++) {
4343 System .out .printf ("%s: %s%n" ,
4444 Plant .LifeCycle .values ()[i ], plantsByLifeCycleArr [i ]);
4545 }
4646
47- // Using an EnumMap to associate data with an enum (Page 172 )
47+ // 코드 37-2 EnumMap을 사용해 데이터와 열거 타입을 매핑한다. (227쪽 )
4848 Map <Plant .LifeCycle , Set <Plant >> plantsByLifeCycle =
4949 new EnumMap <>(Plant .LifeCycle .class );
5050 for (Plant .LifeCycle lc : Plant .LifeCycle .values ())
@@ -53,11 +53,11 @@ public static void main(String[] args) {
5353 plantsByLifeCycle .get (p .lifeCycle ).add (p );
5454 System .out .println (plantsByLifeCycle );
5555
56- // Naive stream-based approach - unlikely to produce an EnumMap! (Page 172 )
56+ // 코드 37-3 스트림을 사용한 코드 1 - EnumMap을 사용하지 않는다! (228쪽 )
5757 System .out .println (Arrays .stream (garden )
5858 .collect (groupingBy (p -> p .lifeCycle )));
5959
60- // Using a stream and an EnumMap to associate data with an enum (Page 173 )
60+ // 코드 37-4 스트림을 사용한 코드 2 - EnumMap을 이용해 데이터와 열거 타입을 매핑했다. (228쪽 )
6161 System .out .println (Arrays .stream (garden )
6262 .collect (groupingBy (p -> p .lifeCycle ,
6363 () -> new EnumMap <>(LifeCycle .class ), toSet ())));
0 commit comments