所以这里是情况:
我需要为特定日期注册人们的投票。简而言之,提出一个日期,人们投票选择他们想要的日期。
数据结构如下:
我的问题是:如何使用 Java Streams 获取最多“YES”日期。
感谢您的帮助!
数据结构如下:
private HashMap<LocalDateTime, Set<Vote>> votes;
投票是:
public class Vote {
private String name;
private VoteType vote;
public Vote(String name, VoteType vote) {
super();
this.name = name;
this.vote = vote;
}
}
VoteType是一个枚举类型:
public enum VoteType {YES, NO, MAYBE}
现在我已经创建了一个流,返回可用性(VoteType)的投票数量:
public Map<LocalDateTime, Integer> voteCount(VoteType targetVote) {
return this.votes.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, e -> new Integer(
e.getValue().stream().filter(v -> v.getVote() == targetVote).collect(Collectors.toList()).size())));
}
我的问题是:如何使用 Java Streams 获取最多“YES”日期。
/* Returns the date that got the most 'YES' votes */
public LocalDateTime winningDate() {
// TODO
}
感谢您的帮助!
MAYBE
投票类型是否有获胜日期的值? - Mohsennew Integer(…)
的理由。从Java 9开始,这个构造函数已被标记为已弃用。 - Holger