我有对象名单listOfStudents列表: 对象:查找字符串的最大次数 - 领带情况下
private class Students
{
private String name;
private int numberOfTimesComeToSchool;
}
我试图查找出现最多次的学生的姓名和这样做,我用:
listOfStudents.stream().map(Student::getName)collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
如果不系领带,然后我用
Collections.max(collect.entrySet(), Map.Entry.comparingByValue()).getKey();
确定发生次数最多的名称。
但是可能会发生这样的情况,即学生的名字中有一个关系,在这种情况下,我想选择最多次来到学校的学生姓名,即具有学生姓名numberOfTimesComeToSchool中所有名称的最高总和。
例如:
Student 1: Name: Hello numberOfTimesComeToSchool: 1
Student 2: Name: Hello numberOfTimesComeToSchool: 4
Student 3: Name: Trial numberOfTimesComeToSchool: 2
Student 4: Name: Trial numberOfTimesComeToSchool: 2
Student 5: Name: NeedThis numberOfTimesComeToSchool: 2
Student 6: Name: NeedThis numberOfTimesComeToSchool: 2
在这种情况下,正确的答案应该是你好作为numberOfTimesComeToSchool的总和是名字的只有4个,其余5时,即使所有的名字都发生在同一数量的时代。
任何与此有关的帮助将被处理。
我将如何能够使用呢?我从来不知道学生的名字。 – Massa
调用'HashBag.uniqueSet()'并迭代它。文档:https://commons.apache.org/proper/commons-collections/apidocs/org/apache/commons/collections4/bag/HashBag.html – ifly6