Priority Queues

本篇文章主要介绍优先队列(Priority Queue)和嵌套类(nested class)的基本用法。
接下来的文章会介绍在实现例如Greedy Best First Search和AStar搜索算法中使用priority queue。

Priority Queue

  • In priority queue:

  • The order of the queue is controlled by implementing Comparable.

Nested Class: Java嵌套类

  • if you make an inner class static, this is commonly called a nested class.

  • create an inner class, you don’t need an outer-class object.

  • you can not access a non-static outer-class object from an object of a nested class.

Example 1 (without nested class)

  • Here is an example to show how we can specify order using Priority Queue.

/**
 * an example of Priority Queue
 * Created by HuGuodong on 2017/10/25.
 */

public class ToDoList extends PriorityQueue<ToDoList.ToDoItem>{
     class ToDoItem implements Comparable<ToDoItem> {
            private char primary;
            private int secondary;
            private String item;
            public ToDoItem(char pri, int sec, String i) {
                this.primary = pri;
                this.secondary = sec;
                this.item = i;
            }

         /**
          * order by the first arg, then the second arg
          * @param o
          * @return
          */
         @Override
            public int compareTo(ToDoItem o) {
                if (primary > o.primary) {
                    return 1;
                }
                if (primary == o.primary) {
                    if (secondary > o.secondary) {

                        return 1;
                    } else if (secondary == o.secondary) {
                        return 0;
                    }
                }
                return -1;
            }
            public String toString() {
                return Character.toString(primary) + secondary + ": " + item;
            }
        }
        public void add(char pri, int sec, String item){
            super.add(new ToDoItem(pri, sec, item));
        }

    public static void main(String[] args){
        ToDoList toDoList = new ToDoList();
        toDoList.add('c', 16,"Fog");
        toDoList.add('c', 3,"Dog");
        toDoList.add('a', 2,"Aog");
        while(!toDoList.isEmpty()){
            // remove is the same as poll, except its throw Exception, when it's empty.
            System.out.println(toDoList.remove());
            //System.out.println(toDoList.poll());
        }
//            result:
//            a2: Aog
//            c3: Dog
//            c16: Fog
    }
}

Example 2 (nested class)

  • Here is an example of using static inner class to implement PriorityQueue.
public class MyPriorityQueue extends PriorityQueue<MyPriorityQueue.Student>{

    public static void main(String[] args){
        MyPriorityQueue que = new MyPriorityQueue();
        que.add(new Student(100, "guodong_hu"));
        que.add(new Student(99, "xie"));
        que.add(new Student(1, "lin"));
        while (!que.isEmpty()){
            System.out.println(que.poll());
        }
    }

    static class Student implements Comparable<Student>{
        Integer no;
        String name;
        public Student(Integer no, String name){
            this.no = no;
            this.name = name;
        }

        @Override
        public int compareTo(Student stu) {
            int result;
            if(no > stu.no){
                result = 1;
            }else if(no == stu.no){
                result = 0;
            }else{
                result = -1;
            }
            return result;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "no=" + no +
                    ", name='" + name + '\'' +
                    '}';
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值