赞
踩
PriorityQueue 虽然叫 优先级队列,但他底层是用heap实现的
代码中使用了Lambda表达式。不会的朋友可以去b站看尚硅谷康师的java课程,在最后的java8新特性里有讲。Lanmbda用了都说好。
@Test public void testPriorityQue(){ //大根堆。必须自己定义比较器。默认是小根堆。。 PriorityQueue<Integer> heap = new PriorityQueue<>((a, b) -> -Integer.compare(a, b)); heap.add(8); heap.add(48); heap.add(-9); heap.add(10); heap.add(19); System.out.println(heap.peek());//根节点 48 while (!heap.isEmpty()){ System.out.println(heap.poll()); } /** * 48 * 48 * 19 * 10 * 8 * -9 */ System.out.println("==================="); //不传比较器,默认是小根堆。 PriorityQueue<Integer> heap2 = new PriorityQueue<>(); heap2.add(8); heap2.add(48); heap2.add(-9); heap2.add(10); heap2.add(19); System.out.println(heap2.peek());//根节点 -9 while (!heap2.isEmpty()){ System.out.println(heap2.poll()); } /** * -9 * 8 * 10 * 19 * 48 */ }
自定义类:Stu
public class Stu{ private String name; private int id; private int age; public Stu(String name, int id, int age) { this.name = name; this.id = id; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getId() { return id; } public void setId(int id) { this.id = id; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @Override public String toString() { return "Stu{" + "name='" + name + '\'' + ", id=" + id + ", age=" + age + '}'; } }
@Test public void testPriorityQue(){ /* PriorityQueue<Stu> stus = new PriorityQueue<>( //(s1,s2)->Integer.compare(s1.getId(),s2.getId())//升序 //(s1,s2)->-Integer.compare(s1.getId(),s2.getId())//逆序 );*/ //等价于 PriorityQueue<Stu> stus = new PriorityQueue<>( Comparator.comparingInt(Stu::getId)//升序 //Comparator.comparingInt(Stu::getId).reversed()//逆序 ); Stu s1 = new Stu("张三", 3, 88); Stu s2 = new Stu("李四三", 78, 78); Stu s3 = new Stu("钟离", 90, 18); Stu s4 = new Stu("左浏", 0, 48); Stu s5 = new Stu("李宏伟", 93, 18); Stu s6 = new Stu("唐一", 10, 88); stus.add(s1); stus.add(s2); stus.add(s3); stus.add(s4); stus.add(s5); stus.add(s6); System.out.println(stus.peek());//默认是小根堆。所以结果为是s4的信息 System.out.println("全部弹出打印:"); while (!stus.isEmpty()){ System.out.println(stus.poll()); } }
优先按年龄逆序,若年龄一样,则按id升序。若年龄和id都一样,则按name升序
@Test public void testPriorityQue(){ //优先按年龄逆序,若年龄一样,则按id升序。若年龄和id都一样,则按name升序 //创建三个比较器。 Comparator<Stu> comByAge = Comparator.comparingInt(Stu::getAge).reversed(); Comparator<Stu> comById = Comparator.comparingInt(Stu::getId); Comparator<Stu> comByName = Comparator.comparing(Stu::getName); PriorityQueue<Stu> stus = new PriorityQueue<>( comByAge.thenComparing(comById).thenComparing(comByName) ); //为了演示效果,测试数据特地设计一下.这只是为了学习怎么用,开发中id肯定不会一样。 Stu s1 = new Stu("ab", 3, 88); Stu s2 = new Stu("peter", 90, 78); Stu s3 = new Stu("uo", 90, 78); Stu s4 = new Stu("Lui", 90, 78); Stu s5 = new Stu("Zem", 90, 18); Stu s6 = new Stu("ste", 10, 88); Stu s7 = new Stu("mike", 0, 19); stus.add(s1); stus.add(s2); stus.add(s3); stus.add(s4); stus.add(s5); stus.add(s6); stus.add(s7); System.out.println(stus.peek()); System.out.println("全部弹出打印:"); while (!stus.isEmpty()){ System.out.println(stus.poll()); } /** * Stu{name='ab', id=3, age=88} * 全部弹出打印: * Stu{name='ab', id=3, age=88} * Stu{name='ste', id=10, age=88} * Stu{name='Lui', id=90, age=78} * Stu{name='peter', id=90, age=78} * Stu{name='uo', id=90, age=78} * Stu{name='mike', id=0, age=19} * Stu{name='Zem', id=90, age=18} */ }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。