Transform and filter a Java Map with streams

前端 未结 6 1702
既然无缘
既然无缘 2021-01-31 02:40

I have a Java Map that I\'d like to transform and filter. As a trivial example, suppose I want to convert all values to Integers then remove the odd entries.

Map         


        
6条回答
  •  遇见更好的自我
    2021-01-31 03:33

    Here is code by AbacusUtil

    Map input = N.asMap("a", "1234", "b", "2345", "c", "3456", "d", "4567");
    
    Map output = Stream.of(input)
                              .groupBy(e -> e.getKey(), e -> N.asInt(e.getValue()))
                              .filter(e -> e.getValue() % 2 == 0)
                              .toMap(Map.Entry::getKey, Map.Entry::getValue);
    
    N.println(output.toString());
    

    Declaration: I'm the developer of AbacusUtil.

提交回复
热议问题