How do you assign a lambda to a variable in Java 8?

前端 未结 3 895
臣服心动
臣服心动 2020-12-14 13:52

Just playing with the new lambda and functional features in Java 8 and I\'m not sure how to do this.

For example the following is valid:

    Map<         


        
3条回答
  •  天命终不由人
    2020-12-14 14:46

    You have forgotten the generics on your BiFunction:

    public static void main(final String[] args) throws Exception {
        final Map map = new HashMap<>();
        map.put("A", 1);
        map.put("B", 2);
        map.put("C", 3);
        final BiFunction remapper = (k, v) -> v == null ? 42 : v + 41;
        map.compute("A", remapper);
    }
    

    Running:

    PS C:\Users\Boris> java -version
    java version "1.8.0-ea"
    Java(TM) SE Runtime Environment (build 1.8.0-ea-b120)
    Java HotSpot(TM) 64-Bit Server VM (build 25.0-b62, mixed mode)
    

提交回复
热议问题