Created
May 8, 2019 02:10
-
-
Save eleanor-em/411ef0a35c6a3a1482d3908f88643fa4 to your computer and use it in GitHub Desktop.
PriorityQueue example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.ArrayList; | |
import java.util.PriorityQueue; | |
import java.util.Comparator; | |
class Customer { | |
private int timeOnHold; | |
public Customer(int timeOnHold) { | |
this.timeOnHold = timeOnHold; | |
} | |
public int getTimeOnHold() { | |
return timeOnHold; | |
} | |
@Override | |
public String toString() { | |
return Integer.toString(timeOnHold); | |
} | |
} | |
class CustomerComparator implements Comparator<Customer> { | |
@Override | |
public int compare(Customer a, Customer b) { | |
return b.getTimeOnHold() - a.getTimeOnHold(); | |
} | |
} | |
class Main { | |
public static PriorityQueue<Customer> prioritise(ArrayList<Customer> customers) { | |
PriorityQueue<Customer> q = new PriorityQueue<>(new CustomerComparator()); | |
q.addAll(customers); | |
return q; | |
} | |
public static void main(String[] args) { | |
ArrayList<Customer> customers = new ArrayList<>(); | |
customers.add(new Customer(5872)); | |
customers.add(new Customer(413)); | |
customers.add(new Customer(74353)); | |
customers.add(new Customer(435)); | |
PriorityQueue<Customer> q = prioritise(customers); | |
while (q.size() > 0) { | |
System.out.println(q.poll()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment