java TreeMap的用法

TreeMap是一种Map,只是对其进行了排序,每一个TreeMap都有一一对应的key--value  ,其中,将key全部封装在一起,就构成了一个treeset下面通过一个程序说说TreeMap中的函数。

  1. <span style="font-size:14px;">import java.util.TreeMap;  
  2.   
  3. public class Lel implements Comparable<Object> {  
  4.   
  5.     int count;  
  6.      Lel(int count){  
  7.         this.count = count;  
  8.     }  
  9.     public int compareTo(Object object) {  
  10.         // TODO Auto-generated method stub  
  11.         Lel lel = (Lel)object;  
  12.         if(lel.count ==this.count){  
  13.             return 0;  
  14.         }  
  15.         if(lel.count>this.count){  
  16.         return 1;  
  17.     }  
  18.         else {  
  19.             return -1;  
  20.         }  
  21.     }  
  22.     public String toString(){  
  23.         return "Lel(count属性:"+count+")";  
  24.     }  
  25.     public boolean equals(Object objObject){  
  26.         if(this==objObject){  
  27.             return true;  
  28.         }  
  29.         else if(objObject!=null&&objObject.getClass()==Lel.class){  
  30.             Lel lel = (Lel)objObject;  
  31.             if(lel.count==this.count){  
  32.                 return true;  
  33.             }  
  34.         }  
  35.         return false;  
  36.           
  37.     }  
  38.     public static class treeMap{  
  39.         public static void main(String []args){  
  40.         TreeMap treeMap =   new TreeMap<>();  
  41.         treeMap.put(new Lel(1),"嘿嘿,对不对");  
  42.         treeMap.put(new Lel(5),"下午上完课干嘛呢");  
  43.         treeMap.put(new Lel(3), "晚上想看看android");  
  44.         treeMap.put(new Lel(-1), "明天是星期五");  
  45.         System.out.println(treeMap.firstEntry());//返回第最大键值对  
  46.         System.out.println(treeMap.firstKey());//返回最大键key  
  47.         System.out.println(treeMap.lastEntry());//返回最小键值对  
  48.         System.out.println(treeMap.lastKey());//返回最小键  
  49.         System.out.println(treeMap.higherEntry(new Lel(3)));//返回3键值后面的键值对  
  50.         System.out.println(treeMap.higherKey(new Lel(3)));//返回键值3后面的键值  
  51.         System.out.println(treeMap.lowerEntry(new Lel(3)));    //返回上一个键值对  
  52.         System.out.println(treeMap.lowerKey(new Lel(3)));//返回上一个key  
  53.         System.out.println(treeMap.subMap(new Lel(3), new Lel(1)));//返回键值从3到1 的子map  
  54.         System.out.println(treeMap.subMap(new Lel(3), truenew Lel(1), true));//这个和上面一样,只是看是否包括  
  55.         System.out.println(treeMap.tailMap(new Lel(1)));//返回比你选择的key小的键值对  
  56.         System.out.println(treeMap.tailMap(new Lel(1), true));  
  57.         System.out.println(treeMap.headMap(new Lel(-1)));//返回比你选择的key大的键值对,不包括本身  
  58.         System.out.println(treeMap.headMap(new Lel(-1), true));//返回比你选择的key大的键值对  
  59.           
  60.     }  
  61. }  
  62. </span>  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值