Code:
import java.util.PriorityQueue;
public class practical24 {
public static void main(String[] args) {
MyPriorityQueue<String> queue= new MyPriorityQueue<>();
queue.add("ABC");
queue.add("PQR");
queue.add("MNO");
queue.add("XYZ");
System.out.println("Queue: \n"+queue);
try {
MyPriorityQueue<String> queue1=(queue.clone());
System.out.println("After Cloning to another Queue:");
System.out.println(queue1);
} catch (CloneNotSupportedException e) {
System.out.println("Error");
}
}
public static class MyPriorityQueue<E> extends PriorityQueue<E> implements Cloneable{
public MyPriorityQueue<E> clone() throws CloneNotSupportedException{
MyPriorityQueue<E> clone= new MyPriorityQueue<>();
this.forEach(clone::offer);
return clone;
}
}
}
Comments
Post a Comment