@@ -0,0 +1,1495 @@
+package effectivejava.chapter7;%0D%0A%0D%0Aimport static org.junit.Assert.assertEquals;%0D%0A%0D%0Aimport java.util.HashMap;%0D%0Aimport java.util.Map;%0D%0Aimport java.util.stream.IntStream;%0D%0A%0D%0Aimport org.junit.Test;%0D%0A%0D%0Apublic class MethodReferencesTest %7B%0D%0A%09@Test%0D%0A%09public void mergeWithMethodRef() %7B%0D%0A%09%09Map%3CInteger,Integer%3E keyCountMap = new HashMap%3C%3E();%0D%0A%09%09%0D%0A%09%09for(int i = 0; i %3C 10; i++) %7B%0D%0A%09%09%09keyCountMap.merge(i, 1, (count, incr) -%3E count + incr);%0D%0A%09%09%7D%0D%0A%0D%0A%09%09IntStream.range(2, 6).forEach(i -%3E keyCountMap.merge(i, 1, (count, incr) -%3E count + incr));%0D%0A%09%09IntStream.range(3, 7).forEach(i -%3E keyCountMap.merge(i, 1, (count, incr) -%3E count + incr));%0D%0A%09%09%0D%0A%09%09System.out.printf(%22map 1 size=%25d, map=%25s%5Cn%22, keyCountMap.size(), keyCountMap);%0D%0A%0D%0A%09%09assertEquals((Integer) 3, (Integer) keyCountMap.get(5)); %0D%0A%09%09assertEquals((Integer) 1, (Integer) keyCountMap.get(1)); %0D%0A%09%09assertEquals((Integer) 1, (Integer) keyCountMap.get(9)); %0D%0A%0D%0A%09%09// map 2 uses method reference instead of a lambda%0D%0A%09%09Map%3CInteger,Integer%3E keyCountMap2 = new HashMap%3C%3E();%0D%0A%09%09%0D%0A%09%09for(int i = 0; i %3C 10; i++) %7B%0D%0A%09%09%09keyCountMap2.merge(i, 1, Integer::sum);%0D%0A%09%09%7D%0D%0A%0D%0A%09%09IntStream.range(2, 6).forEach(i -%3E keyCountMap2.merge(i, 1, Integer::sum));%0D%0A%09%09IntStream.range(3, 7).forEach(i -%3E keyCountMap2.merge(i, 1, Integer::sum));%0D%0A%09%09%0D%0A%09%09System.out.printf(%22map 2 size=%25d, map=%25s%5Cn%22, keyCountMap2.size(), keyCountMap2);%0D%0A%0D%0A%09%09// two maps should be the same%0D%0A%09%09for(Integer k : keyCountMap.keySet()) %7B%0D%0A%09%09%09assertEquals(keyCountMap.get(k), keyCountMap2.get(k));%0D%0A%09%09%7D%0D%0A%09%7D%0D%0A%7D%0D%0A

test using method ref instead of a lambda