• java优先级队列PriorityQueue


    说明

    队列是java高级开发中常用开发技术,普通队列弹出顺序默认FIFO(先进先出)
    优先级队列可以默认或指定算法,自动约束数据弹出顺序,本文记录jdk自带非线程安全优先级队列PriorityQueue
    PriorityBlockingQueue 优先级队列线程安全。

    分享

    PriorityQueue

    • 排序默认升序
      • 数字从0增加
      • 字符排序规则按照ASCII码,即字符0到9 > 大写字符A到Z > 小写字符a到z 如果字符串首字符一样则依次比较后面的字符判断优先级。

    升序实现

    import java.util.PriorityQueue;
    import java.util.Queue;
    
    public class MainTest {
    public static void main(String[] args) {   
            Queue<Integer> p = new PriorityQueue<>();
            p.offer(1);
            p.offer(2);
            p.offer(5);
            p.offer(4);
            p.offer(3);
    
            System.out.println(p.poll());
            System.out.println(p.poll());
            System.out.println(p.poll());
            System.out.println(p.poll());
            System.out.println(p.poll());
       }
    }
    //输出 1 、2、3、4、5
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    降序实现

    import java.util.PriorityQueue;
    import java.util.Queue;
    
    public class MainTest {
    public static void main(String[] args) {   
            Queue<Integer> p = new PriorityQueue<>(Collections.reverseOrder());
            p.offer(1);
            p.offer(2);
            p.offer(5);
            p.offer(4);
            p.offer(3);
    
            System.out.println(p.poll());
            System.out.println(p.poll());
            System.out.println(p.poll());
            System.out.println(p.poll());
            System.out.println(p.poll());
       }
    }
    //输出 5、4、3、2、1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    自定义优先级

    • 创建学生实体,以平均分为条件,分数越大,优先级越高。
    import java.util.List;
    
    /**
     * 学生类
     */
    public class Student  {
    
        //姓名
        public String name;
        //各科分数
        public List<Integer> score;
    
        public Student (String name ,List<Integer> score){
            this.name=name;
            this.score=score;
        }
    
        public Student (){}
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public List<Integer> getScore() {
            return score;
        }
    
        public void setScore(List<Integer> score) {
            this.score = score;
        }
    
        public int getAvg(){
          return score.stream().reduce(0, Integer::sum)/score.size();
        }
    
        @Override
        public String toString() {
            return "name:"+name+",score:"+score+",平均分:"+getAvg();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 实现类
    import java.util.PriorityQueue;
    import java.util.Queue;
    import java.util.Arrays;
    
    public class MainTest {
    public static void main(String[] args) {   
          Student student1 = new Student("小明", Arrays.asList(77, 78, 90));
          Student student2 = new Student("小王", Arrays.asList(88, 65, 81));
          Student student3 = new Student("小红", Arrays.asList(92, 78, 90));
          Student student4 = new Student("小李", Arrays.asList(44, 98, 88));
    
          Queue<Student> q = new PriorityQueue<>((a, b) -> {
              //计算每个学生分数的平均分,降序排序(即平均分越高优先级越高,越先出队列)
              return b.getAvg() - a.getAvg();
          });
    
          q.offer(student1);
          q.offer(student2);
          q.offer(student3);
          q.offer(student4);
    
          System.out.println(q.poll().toString());
          System.out.println(q.poll().toString());
          System.out.println(q.poll().toString());
          System.out.println(q.poll().toString());
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 输出结果:
    name:小红,score:[92, 78, 90],平均分:86
    name:小明,score:[77, 78, 90],平均分:81
    name:小王,score:[88, 65, 81],平均分:78
    name:小李,score:[44, 98, 88],平均分:76
    
    • 1
    • 2
    • 3
    • 4

    总结

    • 排序队列是队列中重要的一部分,常应用于不同优先级功能的开发。
    • 好好学习,加油!!
  • 相关阅读:
    考研高数背诵类知识点-张宇(不定期更新)
    805.数组的均值分割(回溯+折半搜索+数学)
    AR工业远程巡查系统:实时监控设备状态,及时发现潜在问题
    PCR检测试剂——博迈伦
    HyperTerminal 超级终端设置TCP/IP Client和TCP/IP Server
    再一次整理一下spring框架步骤
    vue-element-admin入门①
    AVR单片机开发10——Nrf2401 模拟spi
    防止F12调试的JS代码
    面向对象设计——原型模式
  • 原文地址:https://blog.csdn.net/qq_22973811/article/details/126620563