首页 > 其他 > 详细

紧接着上篇文章,既然

时间:2017-08-19 00:23:49      阅读:259      评论:0      收藏:0      [点我收藏+]

技术分享


技术分享


注意描述:一个是插入队列(1是可以插入尾部eg一遍的序列尾部追加,2是可以插入中间eg优先队列)

而移除的描素都是删除头部

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

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

    public int getScore(){
         return this.score;
    }
    
    public String toString(){
         return getName() + " : " + getScore(); //还有这种操作
    }
    /*以上的javabean结束*/
    public static void main(String args[]){
        Comparator<test> OrderIsdn =  new Comparator<test>(){ //自己的意愿进行优先级排列的队列的话,
            public int compare(test o1, test o2) {  // 需要实现Comparator接口。
                // TODO Auto-generated method stub
                int numbera = o1.getScore();
                int numberb = o2.getScore();
                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",80);
        test t3 = new test("t3",88);
        test t2 = new test("t2",99);
        test t4 = new test("t4",87); 
        priorityQueue.add(t1);  
        priorityQueue.add(t3);
        priorityQueue.add(t2);
        priorityQueue.add(t4); // add可以添加到尾部,或者其他的如优先队列(就看实现类怎么实现)
        System.out.println(priorityQueue);
        //[t2 : 99, t4 : 87, t3 : 88, t1 : 80]
        while(!priorityQueue.isEmpty())
        System.out.println(priorityQueue.poll().toString());//移除都是头部
        /**
         *  t2 : 99
            t3 : 88
            t4 : 87
            t1 : 80
         */
    }
}

 

紧接着上篇文章,既然

原文:http://www.cnblogs.com/cs-lcy/p/7392708.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!