疯狂JAVA讲义---第七章(下):集合Map(转)

Map用于保存具有映射关系的数据。

Map的子类非常的相似HashMap,LinkedHashMap,SortedMap,TreeMap,EnumMap,只是后缀的不同。

很多人都说Hashtable是线程安全的,HashMap是线程不安全的,这是没错。但Hashtable是个古老的东西,连命名都不规则。现在可以利用Collections工具类来弥补HashMap的线程问题,所以尽量少用,我就不介绍Hashtable了。

先说下LinkedHashMap,他会维护元素的插入顺序,eg

  1. public class TestLinkedHashMap
  2. {
  3.     public static void main(String[] args) 
  4.     {
  5.         LinkedHashMap scores = new LinkedHashMap();
  6.         scores.put("语文" , 80);
  7.         scores.put("数学" , 76);
  8.         scores.put("英文" , 76);
  9.         //遍历scores里的所有的key-value对
  10.         for (Object key : scores.keySet())
  11.         {
  12.             System.out.print(key + "------>");
  13.             System.out.print(scores.get(key) + "");
  14.         }
  15.     }
  16. }

其实Properties类也是Map只是多了点解析.properties和.xml的功能,eg

  1. public class TestProperties
  2. {
  3.     public static void main(String[] args) throws Exception
  4.     {
  5.         Properties props = new Properties();
  6.         //向Properties中增加属性
  7.         props.setProperty("username" , "yeeku");
  8.         props.setProperty("password" , "123456");
  9.         //将Properties中的属性保存到a.ini文件中
  10.         props.store(new FileOutputStream("a.ini") , "comment line");
  11.         //新建一个Properties对象
  12.         Properties props2 = new Properties();
  13.         //向Properties中增加属性
  14.         props2.setProperty("gender" , "male");
  15.         //将a.ini文件中的属性名-属性值追加到props2中
  16.         props2.load(new FileInputStream("a.ini") );
  17.         System.out.println(props2);
  18.     }
  19. }

学习Map的时候大家可以类比下Set,也可以参看我前面的博客,我只是贴点例子作为大家的参考,学习集合没什么好办法,主要是多用。eg

  1. //R类,重写了equals方法,如果count属性相等返回true
  2. //重写了compareTo(Object obj)方法,如果count属性相等返回0;
  3. class R implements Comparable
  4. {
  5.     int count;
  6.     public R(int count)
  7.     {
  8.         this.count = count;
  9.     }
  10.     public String toString()
  11.     {
  12.         return "R(count属性:" + count + ")";
  13.     }
  14.     public boolean equals(Object obj)
  15.     {
  16.         if (this == obj)
  17.         {
  18.             return true;
  19.         }
  20.         if (obj != null 
  21.             && obj.getClass() == R.class)
  22.         {
  23.             R r = (R)obj;
  24.             if (r.count == this.count)
  25.             {
  26.                 return true;
  27.             }
  28.         }
  29.         return false;
  30.     }
  31.     public int compareTo(Object obj)
  32.     {
  33.         R r = (R)obj;
  34.         if (this.count > r.count)
  35.         {
  36.             return 1;
  37.         }
  38.         else if (this.count == r.count)
  39.         {
  40.             return 0;
  41.         }
  42.         else
  43.         {
  44.             return -1;
  45.         }
  46.     }
  47. }
  48. public class TestTreeMap
  49. {
  50.     public static void main(String[] args) 
  51.     {
  52.         TreeMap tm = new TreeMap();
  53.         tm.put(new R(3) , "轻量级J2EE企业应用实战");
  54.         tm.put(new R(-5) , "Struts2权威指南");
  55.         tm.put(new R(9) , "ROR敏捷开发最佳实践");
  56.         System.out.println(tm);
  57.         //返回该TreeMap的第一个Entry对象
  58.         System.out.println(tm.firstEntry());
  59.         //返回该TreeMap的最后一个key值
  60.         System.out.println(tm.lastKey());
  61.         //返回该TreeMap的比new R(2)大的最小key值。
  62.         System.out.println(tm.higherKey(new R(2)));
  63.         //返回该TreeMap的比new R(2)小的最大的key-value对。
  64.         System.out.println(tm.lowerEntry(new R(2)));
  65.         //返回该TreeMap的子TreeMap
  66.         System.out.println(tm.subMap(new R(-1) , new R(4)));

  67.     }
  68. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值