zoukankan      html  css  js  c++  java
  • java 中PriorityQueue优先级队列使用方法

      1、前言

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

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

        如果想实现按照自己的意愿进行优先级排列的队列的话,需要实现Comparator接口。如果不提供Comparator的话,优先队列中元素默认按自然顺序排列,也就是数字默认是小的在队列头,字符串则按字典序排列。

      2、java实现

    import java.text.DecimalFormat;
    import java.util.Comparator;
    import java.util.PriorityQueue;
    import java.util.Queue;
    
    /**
     * @Author:sks
     * @Description:
     * @Date:Created in 10:39 2018/1/11
     * @Modified by:
     **/
    
    //二维平面上一个点
     class point {
        //坐标x
        double x;
    
        //坐标y
        double y;
        public point(double x, double y){
            this.x = x;
            this.y = y;
        }
    }
    
    class PointComparator {
        private   point pointOne;
        private point pointTwo;
        public double distance;
        public PointComparator(point pointOne,point pointTwo)
        {
            this.pointOne = pointOne;
            this.pointTwo = pointTwo;
            computeDistance();
        }
        //计算两点之间距离
        private void computeDistance() {
            double val = Math.pow((this.pointOne.x - this.pointTwo.x),2) +
                    Math.pow((this.pointOne.y - this.pointTwo.y),2);
            this.distance = Math.sqrt(val);
        }
    
    
    }
    public class PriorityQueuep_test {
    
        public static void main(String args[]){
             Comparator<PointComparator> OrderDistance =  new Comparator<PointComparator>(){
                public int compare(PointComparator one, PointComparator two) {
                    if (one.distance < two.distance)
                        return 1;
                    else if (one.distance > two.distance)
                        return -1;
                    else
                        return 0;
                }
            };
    
            //定义一个优先队列,用来排序任意两点之间的距离,从大到小排
            Queue<PointComparator> FsQueue = new PriorityQueue<PointComparator>(10,OrderDistance);
    
            for (int i=0;i<6;i++){
    
                java.util.Random r= new java.util.Random(10);
                point one =new point(i*2+1,i*3+2);
                point two =new point(i*5+2,i*6+3);
                PointComparator nodecomp = new PointComparator(one,two);
    
                DecimalFormat df = new DecimalFormat("#.##");
                FsQueue.add(nodecomp);
            }
            DecimalFormat df = new DecimalFormat("#.###");
            for (int i = 0;i<6;i++){
                System.out.println(df.format(FsQueue.poll().distance));
            }
        }
    
    }
  • 相关阅读:
    [SCM]源码管理 perforce的权限管理
    [BuildRelease]产品和文件版本号
    删除所有的.svn 文件
    [SCM]源码管理 perforce快速入门
    6个Linux chkconfig命令实例 增加,删除,查看和修改services的自动启动选项
    [SCM]源码管理 perforce管理员需要知道的命令
    [SCM]源码管理 perforce与分布式团队的开发
    [SCM]源码管理 perforce命令行高级
    PHP aes加密 mcrypt转openssl问题;
    《Excel与VBA程序设计》第四章更新
  • 原文地址:https://www.cnblogs.com/shaosks/p/8267764.html
Copyright © 2011-2022 走看看