Using a generic class in Java question. Implement a priority queue capable of ho
ID: 3762541 • Letter: U
Question
Using a generic class in Java question.
Implement a priority queue capable of holding objects of an arbitrary type, T, by defining a PriorityQueue class that implements the queue with an ArrayList. A priority queue is a type of list where every item added to the queue also has an associated priority. Define priority in your application so that those items with the largest numerical value have the highest priority. Your class should support the following methods: Add(item priority) - Adds a new item to the queue with the associated priority Remove() - Returns the item with the highest priority and removed it from the queue. If the user attempts to remove from an empty queue return null. For example, if q is a priority queue defined to take strings: q.add("X", 10); q.addfY", 1); q.add("Z", 3); System.out.println(q.remove()); // Returns X System.out. println(q.remove()); // Returns Z System.out. println(q.remove()); // Returns Y Test your queue on data with priorities in various orders (e.g. ascending, descending, mixed). You can implement the priority queue by performing a linear search through the ArrayList.Explanation / Answer
public class SamplePOJO {
String name;
int priority;
/**
* @param name
* @param priority
*/
public SamplePOJO(String name, int priority) {
// TODO Auto-generated constructor stub
this.name = name;
this.priority = priority;
}
/**
* @return Returns the name.
*/
public String getName() {
return name;
}
/**
* @param name The name to set.
*/
public void setName(String name) {
this.name = name;
}
/**
* @return Returns the priority.
*/
public int getPriority() {
return priority;
}
/**
* @param priority The priority to set.
*/
public void setPriority(int priority) {
this.priority = priority;
}
}
public class PriorityQueueExample {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
// natural ordering example of priority queue
Queue<Integer> integerPriorityQueue = new PriorityQueue<Integer>(7);
Random rand = new Random();
for(int i=0;i<7;i++){
integerPriorityQueue.add(new Integer(rand.nextInt(100)));
}
for(int i=0;i<7;i++){
Integer in = integerPriorityQueue.poll();
System.out.println("Processing Integer:"+in);
}
//PriorityQueue example with Comparator
Queue<SamplePOJO> customerPriorityQueue = new PriorityQueue<SamplePOJO>(7, idComparator);
add(customerPriorityQueue);
remove(customerPriorityQueue);
}//Comparator anonymous class implementation
public static Comparator<SamplePOJO> idComparator = new Comparator<SamplePOJO>(){
public int compare(SamplePOJO arg0, SamplePOJO arg1) {
// TODO Auto-generated method stub
return (int)(arg1.getPriority()-arg0.getPriority());
}
};
//utility method to add random data to Queue
private static void add(Queue<SamplePOJO> customerPriorityQueue) {
Random rand = new Random();
for(int i=0; i<7; i++){
int id = rand.nextInt(100);
customerPriorityQueue.add(new SamplePOJO( "Srinivas "+id,id));
}
}
//utility method to poll data from queue
private static void remove(Queue<SamplePOJO> customerPriorityQueue) {
while(true){
SamplePOJO cust = customerPriorityQueue.poll();
if(cust == null) break;
System.out.println("Processing Customer with ID="+cust.getName());
}
}
}
OUTPUT :
Processing Integer:11
Processing Integer:38
Processing Integer:69
Processing Integer:74
Processing Integer:81
Processing Integer:87
Processing Integer:89
Processing Customer with ID=Srinivas 94
Processing Customer with ID=Srinivas 83
Processing Customer with ID=Srinivas 76
Processing Customer with ID=Srinivas 75
Processing Customer with ID=Srinivas 48
Processing Customer with ID=Srinivas 29
Processing Customer with ID=Srinivas 23
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.