您的位置:首页 > 编程语言 > Java开发

优先级队列

2015-10-15 00:00 525 查看
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(TodoItemarg) {



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) {



PriorityQueueTesttoDoList = 

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Java