数据结构:
[
{"id":"1","name":["aa","bb"]},
{"id":"1","name":["bb","cc"]}
]
需求:我们需要将name里面的属性值进行分组统计个数
方法一:
ArrayListpeoples = new ArrayList<>(); peoples.add(new Person("1", Stream.of("aa", "bb").collect(Collectors.toList()))); peoples.add(new Person("2", Stream.of("bb", "cc").collect(Collectors.toList()))); Map collect = peoples.stream().flatMap(people -> people.getName().stream()).collect(Collectors.toMap(k -> k, v -> 1, Integer::sum)); System.out.println(collect);
方法二:
HashMapmap = new HashMap<>(); ArrayList peoples = new ArrayList<>(); peoples.add(new Person("1", Stream.of("aa", "bb").collect(Collectors.toList()))); peoples.add(new Person("2", Stream.of("bb", "cc").collect(Collectors.toList()))); peoples.stream().flatMap(people -> people.getName().stream()).forEach(str -> map.merge(str, 1, Integer::sum)); map.forEach((k, v) -> System.out.println(k + " " + v));



