List分组后加上序号,个数总量 stream().collect(Collectors.groupingBy
需求:
1、给列表每个数据同一个名称的分组的成员加上序号(第几次出现)
2、给列表每个数据同一个名称的分组的成员加上本组总量
即给列表的每个元素按照某一字段进行分组,然后每个分组按照一定顺序进行编号,同时让每个元素知道本组的成员个数
如有列表:
persons.add(new Person("aaa", 6)); persons.add(new Person("bbb", 8)); persons.add(new Person("aaa", 12)); persons.add(new Person("ccc", 20)); persons.add(new Person("ddd", 35)); persons.add(new Person("aaa", 99)); persons.add(new Person("bbb", 67));
最后的结果是:
Person{name='aaa', age=6, time=1, total=3} -->在aaa名字的小组里,这个元素的编号是1,这个组有3个人 Person{name='bbb', age=8, time=1, total=2} Person{name='aaa', age=12, time=2, total=3} Person{name='ccc', age=20, time=1, total=1} Person{name='ddd', age=35, time=1, total=1} Person{name='aaa', age=99, time=3, total=3} Person{name='bbb', age=67, time=2, total=2}
代码如下:
package com.bky.test.group_sort; import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.stream.Collectors; /** * 对列表指定字段进行分组 * 然后给每个分组的数据加上序号,以及组的个数总量 * * @author bky * @version V1.0 * @description: GroupCountTestMain * @date 2022/09/27 */ public class GroupCountTestMain { public static void main(String[] args) { List<Person> persons = new ArrayList<>();//列表 persons.add(new Person("aaa", 6)); persons.add(new Person("bbb", 8)); persons.add(new Person("aaa", 12)); persons.add(new Person("ccc", 20)); persons.add(new Person("ddd", 35)); persons.add(new Person("aaa", 99)); persons.add(new Person("bbb", 67)); // 分组收集 Map<String, List<Person>> collect = persons.stream().collect(Collectors.groupingBy(Person::getName)); System.out.println("分组收集结果:"); collect.forEach((k, v) -> System.out.println(k + v)); //分组收集 统计个数 Map<String, Long> conut = persons.stream() .collect(Collectors.groupingBy(Person::getName, Collectors.counting())); System.out.println("分组统计结果:"); conut.forEach((k, v) -> System.out.println(k + ":" + v)); for (Map.Entry<String, List<Person>> entry : collect.entrySet()) { Long i = 1L; String key = entry.getKey(); List<Person> values = entry.getValue(); for (Person value : values) { value.setTotal(conut.get(key)); // 总数 value.setTime(i++); // 次数 } } System.out.println("将分组统计结果写回源列表:"); persons.forEach((person)->System.out.println(person)); } }
person对象:
package com.bky.test.group_sort; /** * @author bky * @version V1.0 * @description: Person * @date 2022/9/27 */ public class Person { private String name; private Integer age; /** * 第几次出现 */ private Long time; /** * 共几次 */ private Long total; public Person(String name, int age) { this.age = age; this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public Long getTime() { return time; } public void setTime(Long time) { this.time = time; } public Long getTotal() { return total; } public void setTotal(Long total) { this.total = total; } @Override public String toString() { return "Person{" + "name='" + name + '\'' + ", age=" + age + ", time=" + time + ", total=" + total + '}'; } }