(24) Define MYPriorityQueue class that
extends Priority Queue to implement the Cloneable interface and implement
the clone() method to clone a priority queue.
Program Code:
public class OOP_24
{
public static void main(String[] args)
{
MyPriorityQueue<String> queue = new MyPriorityQueue<>();
queue.offer("101");
queue.offer("201");
queue.offer("301");
MyPriorityQueue<String> queue1 = null;
try
{
queue1 = (MyPriorityQueue<String>)(queue.clone());
}
catch (CloneNotSupportedException e)
{
e.printStackTrace();
}
System.out.print(queue1);
}
static class MyPriorityQueue<E> extends PriorityQueue<E> implements Cloneable
{
@Override
public Object clone() throws CloneNotSupportedException
{
MyPriorityQueue<E> clone = new MyPriorityQueue<>();
this.forEach(clone::offer);
return clone;
}
}
}
Output:
No comments:
Post a Comment