优先级队列

        优先级队列是不同于先进先出队列的另一种队列。每次从队列中取出的是具有最高优先权的元素。

  PriorityQueue是从JDK1.5开始提供的新的数据结构接口。

  如果不提供Comparator的话,优先队列中元素默认按自然顺序排列,也就是数字默认是小的在队列头,字符串则按字典序排列。

       由于网上的资料大多将优先级队列各个方法属性,很少有实例讲解的,为方便大家以后使用,我就写了个demo~

      如果想实现按照自己的意愿进行优先级排列的队列的话,需要实现Comparator接口。下面的方法,实现了根据某个变量,来进行优先级队列的建立。

  1. import java.util.Comparator;  
  2. import java.util.PriorityQueue;  
  3. import java.util.Queue;  
  4.   
  5. public class test {  
  6.     private String name;  
  7.     private int population;  
  8.     public test(String name, int population)  
  9.     {  
  10.         this.name = name;  
  11.         this.population = population;  
  12.     }  
  13.     public String getName()  
  14.     {  
  15.          return this.name;  
  16.     }  
  17.   
  18.     public int getPopulation()  
  19.     {  
  20.          return this.population;  
  21.     }  
  22.     public String toString()  
  23.     {  
  24.          return getName() + “ - ” + getPopulation();  
  25.     }  
  26.     public static void main(String args[])  
  27.     {  
  28.         Comparator<test> OrderIsdn =  new Comparator<test>(){  
  29.             public int compare(test o1, test o2) {  
  30.                 // TODO Auto-generated method stub  
  31.                 int numbera = o1.getPopulation();  
  32.                 int numberb = o2.getPopulation();  
  33.                 if(numberb > numbera)  
  34.                 {  
  35.                     return 1;  
  36.                 }  
  37.                 else if(numberb<numbera)  
  38.                 {  
  39.                     return -1;  
  40.                 }  
  41.                 else  
  42.                 {  
  43.                     return 0;  
  44.                 }  
  45.               
  46.             }  
  47.   
  48.               
  49.               
  50.         };  
  51.         Queue<test> priorityQueue =  new PriorityQueue<test>(11,OrderIsdn);  
  52.           
  53.                   
  54.               
  55.         test t1 = new test(“t1”,1);  
  56.         test t3 = new test(“t3”,3);  
  57.         test t2 = new test(“t2”,2);  
  58.         test t4 = new test(“t4”,0);  
  59.         priorityQueue.add(t1);  
  60.         priorityQueue.add(t3);  
  61.         priorityQueue.add(t2);  
  62.         priorityQueue.add(t4);  
  63.         System.out.println(priorityQueue.poll().toString());  
  64.     }  
  65. }  
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;

public class test {
    private String name;
    private int population;
    public test(String name, int population)
    {
        this.name = name;
        this.population = population;
    }
    public String getName()
    {
         return this.name;
    }

    public int getPopulation()
    {
         return this.population;
    }
    public String toString()
    {
         return getName() + " - " + getPopulation();
    }
    public static void main(String args[])
    {
        Comparator<test> OrderIsdn =  new Comparator<test>(){
            public int compare(test o1, test o2) {
                // TODO Auto-generated method stub
                int numbera = o1.getPopulation();
                int numberb = o2.getPopulation();
                if(numberb > numbera)
                {
                    return 1;
                }
                else if(numberb<numbera)
                {
                    return -1;
                }
                else
                {
                    return 0;
                }

            }



        };
        Queue<test> priorityQueue =  new PriorityQueue<test>(11,OrderIsdn);



        test t1 = new test("t1",1);
        test t3 = new test("t3",3);
        test t2 = new test("t2",2);
        test t4 = new test("t4",0);
        priorityQueue.add(t1);
        priorityQueue.add(t3);
        priorityQueue.add(t2);
        priorityQueue.add(t4);
        System.out.println(priorityQueue.poll().toString());
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值