优先级队列



package com.bjwilly.test;
 
import java.util.PriorityQueue;
 
public class PriorityQueueTest extends PriorityQueue<PriorityQueueTest.TodoItem>{
     static class TodoItem implements Comparable<TodoItem>{
         private char primary;
         private int secondary;
         private String item;
         
         public TodoItem(String td, char pri, int sec){
             primary = pri;
             secondary = sec;
             item = td;
         }
         @Override
         public int compareTo(TodoItem arg) {
             if (primary > arg.primary)
                 return + 1 ;
             if (primary == arg.primary)
                 if (secondary > arg.secondary)
                     return + 1 ;
                 else if (secondary == arg.secondary)
                     return 0 ;
             return - 1 ;
         }
         
         public String toString(){
             return Character.toString(primary)+ secondary + ": " + item;
         }
         
     }
     
     public void add(String td, char pri, int sec){
         super .add( new TodoItem(td,pri,sec));
     }
     public static void main(String[] args) {
         PriorityQueueTest toDoList = new PriorityQueueTest();
         toDoList.add( "Empty trash" , 'C' , 4 );
         toDoList.add( "Feed dog" , 'A' , 2 );
         toDoList.add( "Feed bird" , 'B' , 7 );
         toDoList.add( "Mow lawn" , 'C' , 3 );
         toDoList.add( "Water lawn" , 'A' , 1 );
         toDoList.add( "Feed cat" , 'B' , 1 );
         while (!toDoList.isEmpty())
             System.out.println(toDoList.remove());
     }
}

输出结果

A1: Water lawn
A2: Feed dog
B1: Feed cat
B7: Feed bird
C3: Mow lawn
C4: Empty trash
 
http://www.bjwilly.com/archives/202.html
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值