为什么我不能比较两个Hashmaps

时间:2017-04-11 06:21:41

标签: java hashmap queue classcastexception comparable

在我的代码中,我需要有两个值的队列,我将其存储为HashMap中的键和值。但是当我将地图放入队列时,我得到了这个例外:

Exception in thread "main" java.lang.ClassCastException: java.util.HashMap cannot be cast to java.lang.Comparable
        at java.util.PriorityQueue.siftUpComparable(Unknown Source)
        at java.util.PriorityQueue.siftUp(Unknown Source)
        at java.util.PriorityQueue.offer(Unknown Source)
        at java.util.PriorityQueue.add(Unknown Source)
        at com.n2s.StringsPractise.Prime.primeCheck(Prime.java:32)
        at com.n2s.StringsPractise.Randomizer.main(Randomizer.java:22)

以下是我的代码所做的事情以及代码本身的描述:

Randomizer的工作是生成一系列正的随机整数,并通过分布式整数队列将它们发送给Prime。 Primes作业是接收整数并计算整数是否为素数,并通过分布式队列(只是一个java队列实现,不需要实现JMS等)将包含原始数字和布尔值的答案返回给Randomizer; Randomizer将打印到系统中。

Randomizer Class:

    public class Randomizer{

    static Queue<Integer> q = new PriorityQueue<Integer>(); 
    public static Queue<Integer> randomGenerate(){
        int num = 0;
        Random rand = new Random();
        for(int k = 0; k < 10; k++){
        num = rand.nextInt(Integer.MAX_VALUE);
        q.add(num);
        num = 0;
        }
        return q;
    }
    public static void main(String[] args){
        Queue<HashMap<Integer, String>> outQ = Prime.primeCheck(randomGenerate());
        System.out.println(outQ);}
    }
}

Prime Class:

public class Prime{
    Randomizer rn = new Randomizer();
    static HashMap<Integer, String> map = new HashMap<Integer, String>();
    static Queue<HashMap<Integer, String>> primeQ = new PriorityQueue<HashMap<Integer, String>>();

    public static Queue<HashMap<Integer, String>> primeCheck(Queue<Integer> q){
            boolean flag = false;
            Iterator<Integer> itr = q.iterator();
                while(itr.hasNext()){
                    int item = (int) itr.next();
                    int srt = (int) Math.sqrt((double) item);
                    if (item==1) {
                            flag = true;
                        } else{
                        for(int i=2;i<=srt;i++){

                            if(item % i == 0) flag = false;          
                        }
                        flag =  true;
                        }
                    if(flag == true)
                    map.put(item, "Prime");
                    else map.put(item, "Not Prime");

                    primeQ.add(map);

                }
                return primeQ;              
            }
}

1 个答案:

答案 0 :(得分:0)

你的问题是PriorityQueue尝试将两个HashMaps相互比较,但它们没有有意义的比较方式。

我建议您使用这样的值持有者类:

public class Value implements Comparable<Value> {

    int item;
    boolean isPrime;

    public Value(int item, boolean isPrime) {
        this.item = item;
        this.isPrime = isPrime;
    }

    @Override
    public int compareTo(Value o) {
        if (o == null) {
            return 1;
        }
        return this.item - o.item;
    }

    @Override
    public String toString() {
        return "Value{" + "item=" + item + ", isPrime=" + isPrime + '}';
    }
}

然后你的整个代码看起来像这样:(我冒昧地清理它并解决你对我的问题的评论中提到的逻辑问题)

public class DLH {

    public static void main(String[] args) {
        Randomizer ran = new Randomizer();
        Queue<Value> outQ = new Prime().primeCheck(ran.randomGenerate());
        System.out.println(outQ);
    }

    private static class Value implements Comparable<Value> {

        int item;
        boolean isPrime;

        public Value(int item, boolean isPrime) {
            this.item = item;
            this.isPrime = isPrime;
        }

        @Override
        public int compareTo(Value o) {
            if (o == null) {
                return 1;
            }
            return this.item - o.item;
        }

        @Override
        public String toString() {
            return "Value{" + "item=" + item + ", isPrime=" + isPrime + '}';
        }
    }

    private static class Prime {

        Queue<Value> primeQ = new PriorityQueue<>();

        public Queue<Value> primeCheck(Queue<Integer> q) {
            boolean flag;
            Iterator<Integer> itr = q.iterator();
            while (itr.hasNext()) {
                Value val;
                int item = (int) itr.next();
                int srt = (int) Math.sqrt((double) item);
                if (item == 1) {
                    flag = true;
                } else {
                    flag = true;
                    for (int i = 2; i <= srt; i++) {

                        if (item % i == 0) {
                            flag = false;
                        }
                    }
                }
                val = new Value(item, flag);

                primeQ.add(val);

            }
            return primeQ;
        }
    }

    private static class Randomizer {

        public Queue<Integer> randomGenerate() {
            Queue<Integer> q = new PriorityQueue<>();
            int num;
            Random rand = new Random();
            for (int k = 0; k < 10; k++) {
                num = rand.nextInt(Integer.MAX_VALUE);
                q.add(num);
            }
            return q;
        }
    }
}