Say I have a small demo to count the number of times a method is called, e.g.
public class Test {
public static Map<Function<String, Integer>, Integer> map = new HashMap<>();
public Test() {
map.put(this::method1, 0);
map.put(this::method2, 0);
}
public Integer method1(String a) {
Integer time = map.get(this::method1);
map.put(this::method1, time + 1);
return time;
}
public Integer method2(String a) {
Integer time = map.get(this::method2);
map.put(this::method2, time + 1);
return time;
}
}
The code above demoed the idea, but the code doesn't compile. It does not complain at the map.put()
; it complains at the map.get()
parts. Do you have an explanation? As well as a way to fix this (while still using function objects and a map, not two individual integers to do the counting).