Skip to content

Instantly share code, notes, and snippets.

@eleanor-em
Created May 8, 2019 02:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eleanor-em/411ef0a35c6a3a1482d3908f88643fa4 to your computer and use it in GitHub Desktop.
Save eleanor-em/411ef0a35c6a3a1482d3908f88643fa4 to your computer and use it in GitHub Desktop.
PriorityQueue example
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