jdk1.8 - stream常用大全

1,找出list重複的數據

List<String> list = Arrays.asList("112", "113");
Map<String, Long> collect = list.stream().collect(Collectors.groupingBy(User::getName, Collectors.counting()));

System.out.println(collect);

public static List<String> getKeyByValue(Map<String, Long> collect){

    List list = new ArrayList();

    for(Map.Entry entry: collect.entrySet()){

        if(entry.getValue() > 1){

            list.add(entry.getKey().toString())

        }

    }

return list;

}

 

 

2,對list中某元素求和

list.stream().collect(Collectors.summingInt(xxxPo.getCounts));

list.stream().mapToInt().sum(xxxPo::getCounts)

list.stream().map(xxxPo::getMoney).reduce(BigDecimal.ZERO, BigDecimal::add);

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章