public static void main(String[] args) {
List<Integer> list = Arrays.asList(45,23,12,88,5,16);
NavigableSet<Integer> nset = new TreeSet<Integer>(list);
System.out.println(nset);
for(Iterator<Integer> itr = nset.descendingIterator(); itr.hasNext();){
System.out.print(itr.next()+"\t");
}
System.out.println();
out.println(nset.first() + "\t" + nset.last());
//---lower than 88 and most closed
System.out.println(nset.lower(88));
//--- <=
System.out.println(nset.floor(88));
//--- >
System.out.println(nset.higher(56));
//---- >=
System.out.println(nset.ceiling(45));
System.out.println("=============");
NavigableMap<String, Integer> map = new TreeMap<String, Integer>();
map.put("56", 91);
map.put("8", 22);
map.put("86", 3);
map.put("22", 88);
out.println(map.ceilingKey("56"));
out.println(map.ceilingEntry("56"));
//-- others are similarity
out.println(map.firstKey() + "\t" +map.firstEntry());
out.println("Before pollFrist==>");
out.println(map);
map.pollFirstEntry();
out.println("After==>"+map+"\n");
//-----set
out.println(nset);
nset.pollFirst();
out.println(nset+"\n"+"=====================");
//---queue linkedlist 实现了 queue 和 list interface
Queue<String> queue = new LinkedList<String>();
queue.offer("star");
queue.offer("tmac");
queue.offer("kobe");
out.println(queue.size()+"\t"+queue);
//head element
out.println(queue.peek()+"\t"+queue);
//get the head element and delete
out.println(queue.poll()+"\t"+queue);
//--note: 避免使用add remove elment 方法
//----Deque 双端队列
Deque<String> ld = new LinkedList<String>();
Deque<Integer> ad = new ArrayDeque<Integer>();
//---ld
ld.offer("kobe");
ld.offerFirst("frist");
ld.offerLast("last");
out.println(ld);
ld.pollFirst();
out.println(ld);
out.println(ld.peekFirst());
ld.removeFirstOccurrence("kobe");
out.println(ld);
//---------pirorityQueue
PriorityQueue<GregorianCalendar> pq = new PriorityQueue<GregorianCalendar>();
pq.offer(new GregorianCalendar(1906,Calendar.DECEMBER,9));
pq.offer(new GregorianCalendar(1902,Calendar.JULY,9));
pq.offer(new GregorianCalendar(1916,Calendar.DECEMBER,7));
pq.offer(new GregorianCalendar(2206,Calendar.JUNE,9));
for(GregorianCalendar date : pq){
out.println(date.get(Calendar.YEAR));
}
out.println("===========");
while(!pq.isEmpty()){
out.println(pq.remove().get(Calendar.YEAR));
}
JAVA TreeSet TreeMap Queue Deque PriorityQueue
最新推荐文章于 2022-03-22 11:40:18 发布