我想找出最受学生欢迎的体育项目。N是作为参数给出的。
我已经完成了3个步骤,但我对解决方案并不满意。我正试着让它一气呵成
这是我的完整代码和解决方案:
public class Person {
private UUID id;
private String name;
private List<Sport> sports = new ArrayList<>();
//getter and setters + constructor
}这是班级体育节目:
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个体育项目:
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()));
}输出:
Football
Handball
Tennis发布于 2019-11-14 12:08:37
如果您愿意使用第三方库,则可以使用Eclipse Collections中的countByEach和topOccurrences方法。
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方法返回一个MutableBag。topOccurrences方法返回ObjectIntPair为Sport的MutableList。
您还可以将Java Streams与Eclipse Collections中的Collectors2实用程序类一起使用,如下所示:
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的提交者。
发布于 2019-11-14 03:25:34
用于此的单个管道将如下所示:
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)https://stackoverflow.com/questions/58844150
复制相似问题