Java8 stream 中利用 groupingBy 进行多字段分组
1、利用stream对数据进行分组并求和
public static void main(String[] args) { List<String> items = Arrays.asList("apple", "apple", "banana", "apple", "orange", "banana", "papaya"); // Map<String,Long> map = items.stream().collect(Collectors.groupingBy(Function.identity(),Collectors.counting())); Map<String,Long> map = items.stream().collect(Collectors.groupingBy(t->t,Collectors.counting())); System.out.println(JSON.toJSONString(map)); }