I know they have same effect, but why does Integer::intValue
give an error?
public class ResistorColorDuo {
Map<String, Integer> colValueMap;
public ResistorColorDuo(Map<String, Integer> colValueMap) {
colValueMap.put("Black", 0);
colValueMap.put("Brown", 1);
colValueMap.put("Red", 2);
colValueMap.put("Orange", 3);
colValueMap.put("Yellow", 4);
colValueMap.put("Green", 5);
colValueMap.put("Blue", 6);
colValueMap.put("Violet", 7);
colValueMap.put("Grey", 8);
colValueMap.put("White", 9);
this.colValueMap = colValueMap;
}
public int getResistorValue(String... colors) {
return Arrays.stream(colors).map(i -> colValueMap.get(i) *
Math.pow(10, colors.length - (Arrays.asList(colors).indexOf(i) + 1)))
.mapToInt(Integer::intValue) // Here occurs an exception
.sum();
}
}
Above code generated an error:
None-static method can not be referenced from a static context
While it works well like this:
public int getResistorValue(String... colors) {
return Arrays.stream(colors).map(i -> colValueMap.get(i) *
Math.pow(10, colors.length - (Arrays.asList(colors).indexOf(i) + 1)))
.mapToInt(Integer -> Integer.intValue())
// Here is the difference
.sum();
}