JR 精品文章 - Useful Java Maps 2
AD: jr (at) javaresearch.org


首页 | 动态 | 文章 | FAQ  | 新闻 | 下载 | 代码 | 工作 | 调查 | 术语 | 站点 | 图书 | 论坛 | 帮助 | 全部  

TOP | 交流 | 软件 | 专栏 | 开源 | 译/著 | 源码 | API  | 推荐 | FTP  | 积分 | 统计 | 搜索 | Blog | 我们  
首页 » 研究文集 » J2SE综合 搜索标题相关文章 搜索标题相关文章    评论此文章 发表评论     开始监控此文章 开始监控   加入收藏夹  加入收藏夹
Useful Java Maps 2
ccyu 整理   更新:2006-11-09 01:19:26  版本: 1.0   

In my previous post [link: http://www.javaresearch.org/article/57511.htm], I pointed out some useful Java Maps like case-insensitive map, etc. In this post I will continue give you some other useful Java Maps. They are fixed size map, lazy map and predicate map.


1.    Fixed size map. As the name claims, this map is a map with fixed size. You can change the content of the map but you cannot add or remove key value pair of the map. Look at the following example. 

  1. package usefullmap;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import org.apache.commons.collections.map.FixedSizeMap;
  5. public class CommonMaps4 {
  6.     public static void testFixedSizeMap() {
  7.         System.out.println("===== testFixedSizeMap =====");
  8.         HashMap map = new HashMap();
  9.         map.put("1""One");
  10.         map.put("2""Two");
  11.         map.put("3""Three");
  12.         
  13.         Map fsm = FixedSizeMap.decorate(map);
  14.         System.err.println(""+fsm);
  15.         fsm.put("1""Uno");
  16.         System.err.println(""+fsm);
  17.         try {
  18.             fsm.put("4""Four");
  19.         }
  20.         catch (Exception e) {
  21.             System.err.println("Ha! You can NOT put any more!!");
  22.             e.printStackTrace();
  23.         }
  24.         try {
  25.             fsm.remove("3");
  26.         }
  27.         catch (Exception e) {
  28.             System.err.println("Ha Ha! You can NOT remove key either!");
  29.             e.printStackTrace();
  30.         }
  31.     }
  32.     public static void main(String[] a) throws Exception {
  33.         testFixedSizeMap();
  34.     }
  35. }


Note that you can only use the static method decorate method to initialize a fixed size map from an existing map. The output is as follows. Of course you need Jakarta commons collection jar file in your CLASSPATH to compile and run this program.

===== testFixedSizeMap =====
{3=Three, 2=Two, 1=One}
{3=Three, 2=Two, 1=Uno}
Ha! You can NOT put any more!!
java.lang.IllegalArgumentException: Cannot put new key/value pair - Map is fixed size
    at org.apache.commons.collections.map.FixedSizeMap.put(FixedSizeMap.java:109)
    at usefullmap.CommonMaps4.testFixedSizeMap(CommonMaps4.java:40)
    at usefullmap.CommonMaps4.main(CommonMaps4.java:57)
Ha Ha! You can NOT remove key either!
java.lang.UnsupportedOperationException: Map is fixed size
    at org.apache.commons.collections.map.FixedSizeMap.remove(FixedSizeMap.java:128)
    at usefullmap.CommonMaps4.testFixedSizeMap(CommonMaps4.java:48)
    at usefullmap.CommonMaps4.main(CommonMaps4.java:57)


2.    Lazy map. As the name claims, this map is a map does NOT initialize its key value pair until you actually reference it. Let’s see the example.


  1. package usefullmap;
  2. import java.util.Date;
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import org.apache.commons.collections.Factory;
  6. import org.apache.commons.collections.FactoryUtils;
  7. import org.apache.commons.collections.map.LazyMap;
  8. public class CommonMaps5 {
  9.     public static void testLazyMap() {
  10.         System.out.println("===== testLazyMap =====");
  11.         Map lazyMap = LazyMap.decorate(
  12.                 new HashMap(),
  13.                 FactoryUtils.instantiateFactory(StringBuffer.class));
  14.         System.err.println(""+lazyMap);
  15.         lazyMap.get("EmptyBuffer");
  16.         System.err.println(lazyMap);
  17.         Map lazyMap2 = LazyMap.decorate(new HashMap(), new Factory() {
  18.             public Object create() {
  19.                 return new Date();
  20.             }
  21.         });
  22.         System.err.println(""+lazyMap2);
  23.         Object then = lazyMap2.get("then");
  24.         System.err.println(""+lazyMap2);
  25.         try {
  26.             Thread.sleep(2000);
  27.         }
  28.         catch (InterruptedException e) {
  29.             e.printStackTrace();
  30.         }
  31.         Object now = lazyMap2.get("now");
  32.         System.err.println(""+lazyMap2);
  33.     }
  34.     public static void main(String[] a) throws Exception {
  35.         testLazyMap();
  36.     }
  37. }


Note that two instances are initially empty hash map. It is your chance to run it to get the output and think why.
    
3.    Predicate map. This map is a map produced to meet some criteria or condition for its key value pair. Let’s see the following example:


  1. package usefullmap;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import org.apache.commons.collections.Predicate;
  5. import org.apache.commons.collections.PredicateUtils;
  6. import org.apache.commons.collections.map.PredicatedMap;
  7. public class CommonMaps6 {
  8.     public static void testPredicatedMap() {
  9.         System.out.println("===== testPredicatedMap =====");
  10.         HashMap map = new HashMap();
  11.         map.put("1""One");
  12.         map.put("2""Two");
  13.         map.put("3""Three");
  14.         
  15.         Predicate isStringPredicate = new Predicate() {
  16.             public boolean evaluate(Object object) {
  17.                 if (object == null)
  18.                     return true;
  19.                 if (object.getClass() == String.class)
  20.                     return true;
  21.                 return false;
  22.             }
  23.         };
  24.         Map pm = PredicatedMap.decorate(map,//origin map
  25.                 PredicateUtils.notNullPredicate(), //key predicate
  26.                 isStringPredicate  //value predicate
  27.         );
  28.         System.err.println(""+pm);
  29.         pm.put("4""Four");
  30.         pm.put("null"null);
  31.         System.err.println(""+pm);
  32.         
  33.         try {
  34.             pm.put("5"new Integer(5)); //try to insert integer as value
  35.         }
  36.         catch (IllegalArgumentException e) {
  37.             System.err.println("Ha! You can NOT put integer as value any more!!");
  38.             e.printStackTrace();
  39.         }
  40.     }
  41.     public static void main(String[] a) throws Exception {
  42.         testPredicatedMap();
  43.     }
  44. }


Note that here we use a most general predicate for key (non null value) and string type for its value. The following is the output. Don’t forget add Jakarta commons collections jar file to your CLASSPATH.

===== testPredicatedMap =====
{3=Three, 2=Two, 1=One}
{null=null, 3=Three, 2=Two, 4=Four, 1=One}
Ha! You can NOT put integer as value any more!!
java.lang.IllegalArgumentException: Cannot add value - Predicate rejected it
    at org.apache.commons.collections.map.PredicatedMap.validate(PredicatedMap.java:135)
    at org.apache.commons.collections.map.PredicatedMap.put(PredicatedMap.java:165)
    at usefullmap.CommonMaps6.testPredicatedMap(CommonMaps6.java:56)
    at usefullmap.CommonMaps6.main(CommonMaps6.java:65)





版权声明  
本篇文章对您是否有帮助?  投票:         投票结果:     12       0
作者其它文章: 作者全部文章
评论人:十字刀客 发表时间: Thu Nov 09 09:40:29 CST 2006
不错,很有帮助。
评论人:shugengen 发表时间: Fri Nov 10 15:05:08 CST 2006
开阔了视野,原来commons下有这么多好东西,要好好学习一下了。
评论人:mrou2001 发表时间: Thu Dec 07 14:23:24 CST 2006
加油啊,支持[java]

这个文章共有 3 条评论
主题: 理解JavaHelp结构的好处 上一篇文章
返回文章列表 返回〔J2SE综合〕
下一篇文章 主题: J2SE使用String.splt时候应该注意的一个问题


文字广告链接
        自主、快速定制基于JAVA的B/S业务系统          重量级企业在线自定义WEB报表平台
        Excel制表、零代码发布、打印、图表结合——快逸报表,免费、稳定、功能强大的java工具
        技术圈: 关于Java、dotNet、PHP、Ruby、奇客、Web2.0等更多资讯博客精选文章

关于 JR  |  版权声明  |  联系我们 

©2002-2006 JR 版权所有 沪ICP备05019622号