小编Bra*_* B.的帖子

使用Java Streams获取嵌套在HashMap中的大多数元素的集合

所以情况就是这样:我需要在某些日期登记人们的投票.简而言之,提出了一个日期,人们投票支持他们想要的日期.

数据结构如下:

private HashMap<LocalDateTime, Set<Vote>> votes;
Run Code Online (Sandbox Code Playgroud)

投票是:

public class Vote {
    private String name;
    private VoteType vote;

    public Vote(String name, VoteType vote) {
        super();
        this.name = name;
        this.vote = vote;
    }
}
Run Code Online (Sandbox Code Playgroud)

其中VoteType只是一个枚举:

public enum VoteType {YES, NO, MAYBE}
Run Code Online (Sandbox Code Playgroud)

现在我已经创建了一个返回可用性投票数量的流(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())));
}
Run Code Online (Sandbox Code Playgroud)

所以我的问题是:如何使用Java Streams获得最多"YES"的日期.

/* Returns the date that got the most 'YES' votes */
public LocalDateTime winningDate() {
    // TODO
}
Run Code Online (Sandbox Code Playgroud)

感谢您的帮助!

java hashmap set java-8 java-stream

9
推荐指数
1
解决办法
153
查看次数

标签 统计

hashmap ×1

java ×1

java-8 ×1

java-stream ×1

set ×1