11package main .java .multithreading ;
22
3- import java .util .HashMap ;
43import java .util .Map ;
54import java .util .concurrent .TimeUnit ;
5+ import java .util .stream .IntStream ;
66
77public class AppleTree {
88 private final String treeLabel ;
99 private final int numberOfApples ;
1010
1111 public static AppleTree [] newTreeGarden (int size ) {
12- AppleTree [] appleTrees = new AppleTree [size ];
13- for (int i = 0 ; i < appleTrees .length ; i ++) {
14- appleTrees [i ] = new AppleTree ("🌳#" + i );
15- }
16- return appleTrees ;
12+ return IntStream .range (0 , size )
13+ .mapToObj (i -> new AppleTree ("🌳#" + i ))
14+ .toArray (AppleTree []::new );
1715 }
1816
1917 public AppleTree (String treeLabel ) {
@@ -37,12 +35,11 @@ public int pickApples() {
3735 }
3836
3937 private String toLabel (String threadName ) {
40- Map <String , String > threadNameToLabel = new HashMap <>();
41- threadNameToLabel .put ("ForkJoinPool.commonPool-worker-1" , "Alice" );
42- threadNameToLabel .put ("ForkJoinPool.commonPool-worker-2" , "Bob" );
43- threadNameToLabel .put ("ForkJoinPool.commonPool-worker-3" , "Carol" );
44- threadNameToLabel .put ("ForkJoinPool.commonPool-worker-4" , "Dan" );
45-
46- return threadNameToLabel .getOrDefault (threadName , threadName );
38+ return Map .of (
39+ "ForkJoinPool.commonPool-worker-1" , "Alice" ,
40+ "ForkJoinPool.commonPool-worker-2" , "Bob" ,
41+ "ForkJoinPool.commonPool-worker-3" , "Carol" ,
42+ "ForkJoinPool.commonPool-worker-4" , "Dan"
43+ ).getOrDefault (threadName , threadName );
4744 }
4845}
0 commit comments