首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从列表中找到最受欢迎的运动,在Java8的学生列表中

如何从列表中找到最受欢迎的运动,在Java8的学生列表中
EN

Stack Overflow用户
提问于 2019-11-14 03:22:42
回答 2查看 215关注 0票数 3

我想找出最受学生欢迎的体育项目。N是作为参数给出的。

我已经完成了3个步骤,但我对解决方案并不满意。我正试着让它一气呵成

这是我的完整代码和解决方案:

代码语言:javascript
复制
public class Person {

    private UUID id;
    private String name;
    private List<Sport> sports = new ArrayList<>();

   //getter and setters + constructor
}

这是班级体育节目:

代码语言:javascript
复制
public class Sport {

    private String name;

    public Sport(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

这是我的数据和逻辑,从中提炼出最受欢迎的3个体育项目:

代码语言:javascript
复制
    public static void main(String[] args) {

        // most popular sports

        Sport football = new Sport("Football");
        Sport tennis = new Sport("Tennis");
        Sport basketBall = new Sport("BasketBall");
        Sport handball = new Sport("Handball");
        Sport swimming = new Sport("Swimming");
        Sport running = new Sport("Running");
        Sport climbing = new Sport("Climbing");


        List<Person> people = new ArrayList<>();
        people.add(new Person(UUID.randomUUID(), "Bob", Arrays.asList(football, handball)));
        people.add(new Person(UUID.randomUUID(), "Tom", Arrays.asList(football, basketBall, tennis)));
        people.add(new Person(UUID.randomUUID(), "Tim", Arrays.asList(climbing, handball, football)));
        people.add(new Person(UUID.randomUUID(), "Marc", Arrays.asList(football, basketBall)));
        people.add(new Person(UUID.randomUUID(), "Gerard", Arrays.asList(tennis, handball)));
        people.add(new Person(UUID.randomUUID(), "Claudia", Arrays.asList(running, handball)));
        people.add(new Person(UUID.randomUUID(), "Sara", Arrays.asList(football, climbing)));
        people.add(new Person(UUID.randomUUID(), "Laura", Arrays.asList(football)));
        people.add(new Person(UUID.randomUUID(), "Mo", Arrays.asList(football, tennis)));


        //Step 1 - Merge all the sports lists of all students
        List<Sport> allSports = new ArrayList<>();
        for (Person person : people) {
            allSports.addAll(person.getSports());
        }

        // Step 2 - Transfor into a Map with groupBy and count
        Map<Sport, Long> collect = allSports.stream().collect(groupingBy(Function.identity(), counting()));

        // Return top 3 most popular sports
        collect.entrySet().stream()
                .sorted(Map.Entry.<Sport, Long>comparingByValue().reversed())
                .limit(3)
                .forEach(s -> System.out.println(s.getKey().getName()));

    }

输出:

代码语言:javascript
复制
 Football
 Handball
 Tennis
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-11-14 12:08:37

如果您愿意使用第三方库,则可以使用Eclipse Collections中的countByEachtopOccurrences方法。

代码语言:javascript
复制
MutableList<Person> people = Lists.mutable.with(
        new Person(UUID.randomUUID(), "Bob", football, handball),
        new Person(UUID.randomUUID(), "Tom", football, basketBall, tennis),
        new Person(UUID.randomUUID(), "Tim", climbing, handball, football),
        new Person(UUID.randomUUID(), "Marc", football, basketBall),
        new Person(UUID.randomUUID(), "Gerard", tennis, handball),
        new Person(UUID.randomUUID(), "Claudia", running, handball),
        new Person(UUID.randomUUID(), "Sara", football, climbing),
        new Person(UUID.randomUUID(), "Laura", football),
        new Person(UUID.randomUUID(), "Mo", football, tennis));

MutableList<String> top3Names =
        people.countByEach(Person::getSports)
                .topOccurrences(3)
                .collect(pair -> pair.getOne().getName());

MutableList<String> expected =
        Lists.mutable.with("Football", "Handball", "Tennis");

Assert.assertEquals(expected, top3Names);

MutableList类型扩展了List并添加了额外的API。我简化了Person构造函数,以获取Sport的一个var arg数组。countByEach方法返回一个MutableBagtopOccurrences方法返回ObjectIntPairSportMutableList

您还可以将Java Streams与Eclipse Collections中的Collectors2实用程序类一起使用,如下所示:

代码语言:javascript
复制
List<String> top3Names = people.stream()
        .collect(Collectors2.countByEach(Person::getSports))
        .topOccurrences(3)
        .collect(pair -> pair.getOne().getName());

List<String> expected =
        Arrays.asList("Football", "Handball", "Tennis");

Assert.assertEquals(expected, top3Names);

注意:我是Eclipse Collections的提交者。

票数 2
EN

Stack Overflow用户

发布于 2019-11-14 03:25:34

用于此的单个管道将如下所示:

代码语言:javascript
复制
people.stream()
        .flatMap(a -> a.getSports().stream()) // step 1 (stream of Sport)
        .collect(groupingBy(Function.identity(), counting())) // step 2 (map with count)
        .entrySet().stream()
        .sorted(Map.Entry.<Sport, Long>comparingByValue().reversed())
        .limit(3)
        .map(entry -> entry.getKey().getName()) // mapped to speficic type before accessing
        .forEach(System.out::println); // step 3 (print top N entry names)
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58844150

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档