使用HashMap和Java进行投票程序

时间:2015-11-15 07:46:56

标签: java hashmap

您好以下是我到目前为止的代码,但现在我已经停留在最后一小部分了。我已经在HashMap中获得了所有正确的值,但我不确定如何获取与特定值相关联的键。密钥是候选人的唯一标识,而值是总投票数。

我很确定我在网上看到有一种方法可以获得键/值的1:1映射,这样我就可以在知道某个值时检索特定的键但不确定如何执行此操作。

非常感谢帮助。

import java.util.HashMap;

public class VoteCount {

    public static void main(String[] args) {

        /** Hardwire values in for Sample Input 1 */

        /** Sample Input 1 */
        int numberVotes1 = 8;
        HashMap<String, Vote> votes1 = new HashMap<String, Vote>(numberVotes1);

        Vote vote1 = new Vote("123456789");
        Vote vote2 = new Vote("111111111");
        Vote vote3 = new Vote("987654321");
        Vote vote4 = new Vote("111111111");
        Vote vote5 = new Vote("987654321");
        Vote vote6 = new Vote("987654321");
        Vote vote7 = new Vote("987654321");
        Vote vote8 = new Vote("987654321");

        castVote(votes1, vote1);
        castVote(votes1, vote2);
        castVote(votes1, vote3);
        castVote(votes1, vote4);
        castVote(votes1, vote5);
        castVote(votes1, vote6);
        castVote(votes1, vote7);
        castVote(votes1, vote8);

        System.out.println(votes1.size());
        System.out.println(votes1.keySet());
        System.out.println(votes1.entrySet());
        System.out.println(votes1.get("123456789"));
        System.out.println(votes1.get("111111111"));
        System.out.println(votes1.get("987654321"));

        /** Sample Input 2 */
        int numberVotes2 = 2;
        HashMap<String, Vote> votes2 = new HashMap<String, Vote>(numberVotes2);

        Vote vote9 = new Vote("123456789");
        Vote vote10 = new Vote("111111111");

        castVote(votes2, vote9);
        castVote(votes2, vote10);

        System.out.println(votes2.size());
        System.out.println(votes2.keySet());
        System.out.println(votes2.entrySet());
        System.out.println(votes2.get("123456789"));
        System.out.println(votes2.get("111111111"));
    }

    public static class Vote {

        private int vote;
        private String voterIdentification;

        public Vote(String s) {
            vote = 1;
            voterIdentification = s;
        }

        /** Create getter/setter for any private variables */
        public int getVote() { return vote; }
        public void setVote(int _vote) { this.vote = _vote; }

        public String getIdentification() { return voterIdentification; }
    }

    /**Method used for casting vote */
    public static void castVote(HashMap _votes, Vote _vote) {
        if(_votes.containsKey(_vote.getIdentification())) {
            int vote = (int) _votes.get(_vote.getIdentification());
            _votes.put(_vote.getIdentification(), vote + 1);
        }
        else {
            _votes.put(_vote.getIdentification(), _vote.getVote());
        }
    }

}

输出如下(出于显示目的,显示所有值都正确):

3
[111111111, 987654321, 123456789]
[111111111=2, 987654321=5, 123456789=1]
1
2
5
2
[111111111, 123456789]
[111111111=1, 123456789=1]
1
1

2 个答案:

答案 0 :(得分:0)

HashMap是一种允许存储元素对的虚线结构。每个键都与一个值相关联,但是相同的值可以映射到更多的键。所以1:1映射只是从键到值。您无法确定从给定值开始检索唯一键。

答案 1 :(得分:0)

&#34;获得键/值&#34的1:1映射的方法;你没有1:1,因为任何数量的候选人都可以达到一定的投票数,因此你有n:1。因此,如果您反转计算投票的哈希映射,则反向必须为Map<Integer,Set<String>>。您可以通过迭代原始Map的EntrySet来创建此Map:

Map<String,Integer> cand2count = ...;
Map<Integer,Set<String>> count2cands = new HashMap<>();
for( Map.Entry<String,Integer> entry: cand2count.entrySet() ){
     count2cands.computeIfAbsent(entry.getValue(),
                                 k -> new HashSet<String>()).add(entry.getKey());
}