Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy of a PriorityQueue without Interfere with the original PriorityQueue

I am trying to copy a PriorityQueue object.

My goal is to change some object of my Copy without modifying my original PriorityQueue

In order to do so I did a copy of my PriorityQueue and I delete a value of my copy, but when I check if my PriorityQueue is stil the same, unfortunately the original PriorityQueue was changed as well... If you have any suggestion it will be welcome.

Please find an example of what I tried :

public class PQExample
{
    public int id;
    public int price;
    public String name;
    public long date;

        public PQExample(int id, int price, String name, long time)
        {
            this.id = id;
            this.price = price;
            this.name = name;
            this.date = time;
        }

        public static void main(String[] args)
        {
            PriorityQueueComparator pqc = new PriorityQueueComparator();
            PriorityQueue<PQExample> PQ = new PriorityQueue<PQExample>(pqc);
            int setID = 1000;
            int setDate = 0;
            PQ.add(new PQExample(setID++, 24 , "Mat", setDate++));
            PQ.add(new PQExample(setID++, 25 , "Tommy", setDate++));
            PQ.add(new PQExample(setID++, 22 , "Kate", setDate++));
            PQ.add(new PQExample(setID++, 26 , "Mary", setDate++));
            PQ.add(new PQExample(setID++, 24 , "Ronny", setDate++));
            PQExample valueToDel = new PQExample(1000,22,"Mat",0);

            PriorityQueue<PQExample> PQCopy = new PriorityQueue<PQExample>();
            PQCopy = PQ;

            //Now I want to delete only in PQCopy and not in PQ    
            PQCopy.remove(valueToDel);

            //Unfortunately Mat was deleted of both objects : PQ and PQCopy...
            for (int i = 0; i < 4; i++) {
                System.out.println("Queue in: " + i + " is " + PQ.peek().name + " with the price of " + PQ.peek().price);
                PQ.poll();
            }
        }
@Override
public boolean equals(Object o)
{
    if (o instanceof PQExample)
    {
        PQExample pqExample = (PQExample)o;
        return id == pqExample.id;
    }
    return false;
}


class PriorityQueueComparator implements Comparator<PQExample>
{
    @Override
    public int compare(PQExample o1, PQExample o2) {
        if (o1.price < o2.price){return 1;}else if (o1.price > o2.price){return -1;}
        else
        {if (o1.date<o2.date){return -1;}else{return 1;}}
    }
}
like image 869
Thomas Th. Avatar asked Oct 27 '25 04:10

Thomas Th.


1 Answers

PQcopy is just a second reference to the actual PriorityQueue object. You want to clone the original PQ instead of just assigning the reference. PriorityQueue has a constructor which does this for you:

PriorityQueue<PQExample> PQCopy = new PriorityQueue<PQExample>(PQ)

Incidentally, you should follow capitalization conventions for Java - starting variable names with capital letters is frowned upon.

like image 194
pjs Avatar answered Oct 29 '25 19:10

pjs