JAVA TreeSet TreeMap Queue Deque PriorityQueue


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));
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值