list stream将对象List中的某个字段取出放在新的集合中

public class StreamGetCol {
    
    
    public static void main(String[] args) {
    
    
        List<Map<String, Object>> oldList = new ArrayList<>();
        Map<String, Object> map1 = new HashMap<>();
        map1.put("name", "java");
        map1.put("price", 18800);
        oldList.add(map1);

        Map<String, Object> map2 = new HashMap<>();
        map2.put("name", "python");
        map2.put("price", 16800);
        oldList.add(map2);

        Map<String, Object> map3 = new HashMap<>();
        map3.put("name", ".net");
        map3.put("price", 14800);
        oldList.add(map3);

        List<Integer> newList = new ArrayList<>();
        newList = oldList.stream().map(item -> Integer.parseInt(item.get("price").toString())).collect(Collectors.toList());
        System.out.println(newList);

    }
}

运行结果如下:

[18800, 16800, 14800]

猜你喜欢

转载自blog.csdn.net/m0_37899908/article/details/130914123