【问题标题】:Java 8 streams library | Grouping elements of a list and counting [duplicate]Java 8 流库 |对列表的元素进行分组并计数[重复]
【发布时间】:2015-10-30 07:19:57
【问题描述】:

我有一个list<Person>,我想要将Person.age 的列表元素分组到一个HashMap 中,其中键是Person.age,值是组中元素的数量。

Class Person { String name; int age; }

然后我使用以下行对它们进行分组:

Map<Integer,List<Person>> groupByAgeMap = personList.stream() .collect(Collectors.groupingBy(Person::getAge));

但我真正想要的是一个 Map,其中 map key = Person.age 和 value = 该年龄列表中的元素数。

现在我正在通过以下行实现它:

Map<Integer,Integer> groupByAgeCountMap = new HashMap<>(); groupByAgeMap.forEach((k, v) -> groupByAgeCountMap.put(k, v.size()));

但我在迭代 MAP 时感觉不对。必须有一些直接的方式使用流媒体。任何帮助将不胜感激。

给出完整的例子::

package com.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

class Person {
    String name;
    int age;
    /**
     * @return the name
     */
    public String getName() {
        return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     * @return the age
     */
    public int getAge() {
        return age;
    }
    /**
     * @param age the age to set
     */
    public void setAge(int age) {
        this.age = age;
    }
}

public class GroupPerson {
    public Map<Integer,Integer> getTimesOfferOpened() {
        List<Person> personList = new ArrayList<>();

        Person person1 = new Person();
        person1.setName("Person1");
        person1.setAge(10);

        Person person2 = new Person();
        person2.setName("Person2");
        person2.setAge(10);

        Person person3 = new Person();
        person3.setName("Person3");
        person3.setAge(8);

        personList.add(person1);
        personList.add(person2);
        personList.add(person3);

        Map<Integer,List<Person>> personGroupdByAgeMap = personList.stream().collect(Collectors.groupingBy(Person::getAge));

        // Truncating all the data and returning the map only with count
        Map<Integer,Integer> numberOfPersonCountByAgeMap = new HashMap<>();
        personGroupdByAgeMap.forEach((k, v) -> numberOfPersonCountByAgeMap.put(k, v.size()));
        return numberOfPersonCountByAgeMap;
    }

    public static void main(String args) {
        GroupPerson obj = new GroupPerson();
        System.out.println(obj.getTimesOfferOpened());
    }
}

【问题讨论】:

  • 如果您能提供一个简短但完整的程序来演示问题 - 包括示例数据和预期输出,这将使我们更容易为您提供帮助。然后我们可以测试我们的潜在解决方案,而每个回答者不必编写自己的测试应用程序。
  • 我怀疑你只是想要Collectors.groupingBy(Person::getAge, Collectors::counting)
  • @JonSkeet:我已经用实际课程编辑了我的问题。我可以通过使用上面的代码来实现所需的映射,如下所示{10=2, 8=1} 我想要的是删除迭代personGroupdByAgeMap 以获得numberOfPersonCountByAgeMap。我希望我现在清楚了。
  • 我能够通过以下方式实现它:Map&lt;Integer,Long&gt; personGroupdByAgeMap = personList.stream() .collect(Collectors.groupingBy(Person::getCount, Collectors.counting())) ;
  • 对 - 所以我建议你要么自己添加答案,要么删除问题。

标签: java java-8 java-stream


【解决方案1】:

我能够通过以下方式实现它:

Map&lt;Integer,Long&gt; personGroupdByAgeMap = personList.stream() .collect(Collectors.groupingBy(Person::getCount, Collectors.counting())) ;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-27
    • 2021-04-29
    • 1970-01-01
    相关资源
    最近更新 更多