按优先级排列的集合对象PriorityBuffer用法,来自org.apache.commons.collections.buffers.PriorityBuffer
PriorityBuffer使用比较器Comparator来排列内容,若使用不包含比较器Comparator的PriorityBuffer,此时所有放入PriorityBuffer中的对象都要实现Comparable接口,PriorityBuffer会使对象按照自然顺序排列。
如下,不包含比较器Comparator的PriorityBuffer的使用实例:
import java.util.Iterator;
import org.apache.commons.collections.Buffer;
import org.apache.commons.collections.buffer.PriorityBuffer;
public class BufferTest {
public static void main(String[] args) {
/**
* a PriorityBuffer uses the Comparator to sort its contens,returning the element of
* greatest priority.Using a PriorityBuffer without a Comparator causes the buffer to
* prioritize objects by natural order,casting each object to Comparable and comparing
* objects with CompareTo() method;all objects must implement the Comparable interface
* if no Comparator is supplied.
*/
Buffer priority=new PriorityBuffer();
priority.add(new Long(2));
priority.add(new Long(1));
priority.add( new Long( 20 ) );
priority.add( new Long( 7 ) );
priority.add( new Long( 18 ) );
priority.add( new Long( 1 ) );
// Print the results in priority order
Iterator priorityIterator = priority.iterator( );
while( priorityIterator.hasNext( ) ) {
Long value = (Long) priorityIterator.next();
System.out.println( "Value: " + value );
}
}
}
输出结果如下
Value: 1
Value: 2
Value: 1
Value: 7
Value: 18
Value: 20
PriorityBuffer就像Buffer的实现一样,定义了那个对象被优先remove的机制,接下来说说包含比较器的PriorityBuffer。以急症室举例,一般病人的医治顺序与病人病情的严重程度与病人等待时间相关,严重的有生命危险的病人比一般性病人在看病的顺序上有优先权,假设你有一个Patient病人实例,有name,severity和checkIn属性。病人的严重度severity是从0到10的数字,0最低,10最严重。病人进入候诊室的时间checkIn是在相同严重度下决定优先级的要素。
如下:病人对象
import java.util.Date;
public class Patient {
private String name;
private Integer severity;
private Date checkIn;
public Patient(){
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getSeverity() {
return severity;
}
public void setSeverity(Integer severity) {
this.severity = severity;
}
public Date getCheckIn() {
return checkIn;
}
public void setCheckIn(Date checkIn) {
this.checkIn = checkIn;
}
}
根据病人的情况判断就诊次序的比较器对象如下,实现了Comparator接口:
import java.util.Comparator;
public class PatientPriorityComparator implements Comparator {
@Override
public int compare(Object o1, Object o2) {
int comparison = -1;
if( o1 instanceof Patient &&
o2 instanceof Patient ) {
Patient p1 = (Patient) o1;
Patient p2 = (Patient) o2;
comparison = p2.getSeverity( ).compareTo( p1.getSeverity( ) );
if( comparison == 0 ) {
comparison =
p2.getCheckIn( ).compareTo( p1.getCheckIn( ) );
}
}
return comparison;
}
}
候诊室的实现:
import java.util.Date;
import org.apache.commons.collections.Buffer;
import org.apache.commons.collections.buffer.PriorityBuffer;
public class WaitingRoomApp {
public static void main(String[] args) {
WaitingRoomApp example = new WaitingRoomApp( );
example.start( );
}
public void start(){
Buffer patients =
new PriorityBuffer( new PatientPriorityComparator( ) );
Patient johnDoe1 = new Patient( );
johnDoe1.setName( "John Doe 1" );
johnDoe1.setSeverity( new Integer( 7 ) );
johnDoe1.setCheckIn( new Date( ) );
patients.add( johnDoe1 );
Patient johnDoe2 = new Patient( );
johnDoe2.setName( "John Doe 2" );
johnDoe2.setSeverity( new Integer( 9 ) );
johnDoe2.setCheckIn( new Date( ) );
patients.add( johnDoe2 );
Patient janeDoe3 = new Patient( );
janeDoe3.setName( "Jane Doe 3" );
janeDoe3.setSeverity( new Integer( 9 ) );
janeDoe3.setCheckIn( new Date( ) );
patients.add( janeDoe3 );
while( patients.size( ) > 0 ) {
Patient patient = (Patient) patients.remove( );
System.out.println( "Patient: " + patient.getName( ) );
}
}
}
结果如下:
Patient: John Doe 2
Patient: Jane Doe 3
Patient: John Doe 1