package queue;


import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue ;
import java.util.Random;

// 优先级队列
public class ProQuery {
public static void main(String[] args) {
// insertSort();
protable();
}

// 初识优先级
public static void insertSort(){
Queue<Integer> queue = new PriorityQueue<>(7) ;
Random random =new Random();
for (int i=0;i<7;i++){
queue.add(new Integer(random.nextInt(100)));
}
for (int i=0;i<7;i++){
Integer integer = queue.poll();
System.out.println(integer);
}
}
// 对象优先级 id排序
public static void protable(){
Queue<customer> customerQueue = new PriorityQueue<>(7,idcompara);
for (int i=0;i<7;i++){
int id = new Random().nextInt(100);
customerQueue.add(new customer(id,"name"+i));
}
while (true){
customer customers = customerQueue.poll();
if (null ==customers){
break;
}
System.out.println(customers.getId()+" "+customers.getName());

}

}
// 建立比较级
public static Comparator<customer> idcompara=new Comparator<customer>() {
@Override
public int compare(customer o1, customer o2) {
return (o1.getId()-o2.getId());
}
};

}

class customer{
private int id;
private String name;

public customer(int id, String name) {
this.id = id;
this.name = name;
}

public int getId() {
return id;
}

public String getName() {
return name;
}

public void setId(int id) {
this.id = id;
}

public void setName(String name) {
this.name = name;
}
}
posted on 2021-07-22 17:05  沐雨清晨  阅读(42)  评论(0编辑  收藏  举报