如何从Java 8学生列表中的列表中找到最受欢迎的n种运动

ErE*_*TuS 3 java java-8 java-stream

我想找出学生练习的最热门(N)体育。n作为参数给出。

我已经分3个步骤完成了,但是我对解决方案不满意。我正在努力做到

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

public class Person {

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

   //getter and setters + constructor
}
Run Code Online (Sandbox Code Playgroud)

这是Class Sport:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的数据,也是提取前三项最受欢迎运动的逻辑:

    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()));

    }
Run Code Online (Sandbox Code Playgroud)

输出:

 Football
 Handball
 Tennis
Run Code Online (Sandbox Code Playgroud)

Nam*_*man 5

一个单一的管道看起来像:

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)
Run Code Online (Sandbox Code Playgroud)