Home > Java > javaTutorial > How to implement the java grouping statistics function

How to implement the java grouping statistics function

王林
Release: 2023-04-29 08:52:06
forward
1381 people have browsed it

Let’s take a look at the following case:

//下面是初始化的数据
       List<Student> list = new ArrayList<Student>();
        Student student1 = new Student("李四1", "女", "一班");
        Student student2 = new Student("李四2", "女", "一班");
        Student student3 = new Student("李四3", "女", "一班");
        Student student4 = new Student("李四4", "男", "一班");
        Student student5 = new Student("李四5", "男", "一班");
        Student student6 = new Student("李四6", "男", "二班");
        Student student7 = new Student("李四7", "男", "二班");
        Student student8 = new Student("李四8", "男", "二班");
        Student student9 = new Student("李四9", "男", "二班");
        list.add(student1);
        list.add(student2);
        list.add(student3);
        list.add(student4);
        list.add(student5);
        list.add(student6);
        list.add(student7);
        list.add(student8);
        list.add(student9);
Copy after login

1. Reasonable use of map operations

In actual development, reasonable use of map’s own methods can solve many problems

 for (Student  stu : list) {
            if (!map.containsKey(stu.getProvinceCode())) {
                ArrayList<ArrearageDeal> al = new ArrayList<ArrearageDeal>();
                map.put(stu.getProvinceCode(),  al.add(stu));
            } else {
                map.get(stu.getProvinceCode()).add(stu);
            }
        }
Copy after login

2. Use guava’s Multimap

Multimap<String, Student> mulMap = ArrayListMultimap.create();
for (Student  stu : list) {
       mulMap.put(stu.getGrade,stu);   
}
Copy after login

3. Use the new features of jdk8 – don’t reject new things

After all, java14 has come out, and you still need to know more about the new features of java8

//一行就可以解决
Map<String, List<Student  >> collect = list.stream().collect(Collectors.groupingBy(ArrearageDeal::getGrade));
Copy after login

From the perspective of code volume among the above three, java8 was the simplest. However, in actual development, both 2 and 3 are good choices based on specific scenarios.

Java8 Multiple Field Group Statistics

// 分组统计
Map<String, Long> countMap = records.stream().collect(Collectors.groupingBy(o -> o.getProductType() + "_" + o.getCountry(), Collectors.counting()));
 List<Record> countRecords = countMap.keySet().stream().map(key -> {
    String[] temp = key.split("_");
    String productType = temp[0];
    String country = temp[1];     
    Record record = new Record();
    record.set("device_type", productType);
    record.set("location", country;
    record.set("count", countMap.get(key).intValue());
    return record;
}).collect(Collectors.toList());
Copy after login

The above is the detailed content of How to implement the java grouping statistics function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template