-2

I am just trying to insert some class objects in to a priority queue in java. But getting the error "com.java.split.MyComp cannot be cast to java.util.Collection". I tried different options by passing different parameter to,

PriorityQueue<Node> serverLog = new PriorityQueue<Node>();

Code:

import java.util.Comparator; import java.util.PriorityQueue;

public class Split {

    public static void main(String args[])
    {

        Comparator comparator = new MyComp();  

        PriorityQueue<Node> serverLog = new PriorityQueue<Node>(); 


        Node n1 = new Node(1,"one");
        serverLog.add(n1);

        Node n2 = new Node(1,"two");
        serverLog.add(n2);      <== Error Here (line 22)

        Node n3= new Node(1, "three");
        serverLog.add(n3);

    }

}

public class Node {

    private long timeStamp;
    private String log;

    public Node(long timeStamp, String log)
    {
        this.timeStamp = timeStamp;
        this.log = log;
    }

//getter and setter
}


public class MyComp implements Comparator {

    @Override
    public int compare(Object a, Object b) {

        long aTimeStamp = ((Node) a).getTimeStamp();
            long bTimeStamp = ((Node) b).getTimeStamp();


        if(aTimeStamp == bTimeStamp)
            return 0;
        else if (aTimeStamp < bTimeStamp)
            return 1;
        else
            return -1;
    }
}

but none is working. I am getting the exception,

Exception in thread "main" java.lang.ClassCastException: com.java.split.Node 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.java.split.Split.main(Split.java:19)

There were lot of post suggesting to implement Comparator and override compare method but I could not fix it. I dont know the number of object that I am going to insert. kindly suggest what can be done to make this work?

Thank you!! -Bala

sbala_20
  • 95
  • 2
  • 4
  • 10

1 Answers1

-1

Your exception occurs in your commented code (assuming it wasnt

//PriorityQueue<Node> serverLog = new PriorityQueue<Node>((Collection<? extends Node>) new MyComp());

PriorityQueue doesn't have a constructor that accepts just a Comparator. And you obviously cannot cast a MyComp to a Collection.

I am just trying to insert a class object in to a priority queue in java

Use the constructor that accepts an initialCapacity and a Comparator.

int initialCapacity = ...; // some value
PriorityQueue<String> serverLog = new PriorityQueue<>(initialCapacity , comparator);

if you want to use your custom Comparator.

Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
  • I dont know the value of initialCapacity. It is very large and keep on changing. sorry I mentioned it as "a class object". I changed it – sbala_20 Sep 27 '13 at 03:13
  • @sbala_20 The capacity of the `PriorityQueue` resizes as it's needed. – Sotirios Delimanolis Sep 27 '13 at 03:15
  • Thank you for pointing it out. Whether the priority queue will restructure itself when every time i add new element? Does it slows down the performance? I need to add lot of elements here.. – sbala_20 Sep 27 '13 at 03:35
  • @sbala_20 Glancing at the Oracle source code, it looks like it grows when it needs to. I can't find anywhere where its size is reduced. The `PriorityQueue` use an `Object[]` as its backing data structure. It is extremely fast to copy arrays (which happens when you grow them). – Sotirios Delimanolis Sep 27 '13 at 03:38
  • @sbala_20 If you are preserving the order, then it is not prioritized. – Sotirios Delimanolis Sep 27 '13 at 13:37
  • Is there is anyway that I can preserve the order of the entry. Say for example I have three entry's like(1,"two),(2,two),(1,one) and entering this in same order i need to get output in the order (1,"two") (1,"one") (2,"two") but as of now it is displaying it as, (1,"one") (1,"two") (2,"two"). – sbala_20 Sep 27 '13 at 13:37
  • @sbala_20 As I said, the `PriorityQueue` uses your comparator (or natural ordering) to order the entries you put in. If you want to preserve the order, keep a parallel `List` to which you also add/remove the entries. – Sotirios Delimanolis Sep 27 '13 at 13:41
  • Basically i need to create an sorted collection which allows duplicate as in here, http://stackoverflow.com/questions/12827595/java-sorted-collection-which-allows-duplicates-is-memory-efficient-and-provide – sbala_20 Sep 27 '13 at 13:42
  • @sbala_20 As the answers state, you're better off looking into some 3rd party libraries. – Sotirios Delimanolis Sep 27 '13 at 13:49
  • I created a TreeMap. By this way I can able to achieve my requirement. Whenever I have a duplicate key I will add the value correspond to that key in to the ArrayList and i can retrieve it based on the key. – sbala_20 Sep 27 '13 at 18:44