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


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

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

In Java, the data structure Map is very often used. However, Java itself only provide the limited support for the Map. The most often used implementation is HashMap. If you want the key to be in order, you can use LinkedHashMap. What about you want the key as a string type to be case-insensitive? What about you want the map of which the key is unique as object identity? What about you want the map which you can search the key value from its value?

You might create your own Map to fulfill the above needs. However, Jakarta commons collection library provide all these implement already. The following code snippet shows how:

1.    case-insensitive map: 

package usefullmap;

import org.apache.commons.collections.map.CaseInsensitiveMap;

public class CommonMaps1 {

    public static void testCaseInsensitiveMap() {
        System.out.println("===== testCaseInsensitiveMap =====");
        CaseInsensitiveMap caseInsensitiveMap = new CaseInsensitiveMap();
        caseInsensitiveMap.put("key1", "value1");
        caseInsensitiveMap.put("key2", "value2");
        caseInsensitiveMap.put("KeY1", "value3");

        System.err.println("==================: " + caseInsensitiveMap);
        System.err.println("Value of KEY1: " + caseInsensitiveMap.get("KEY1"));
    }

    public static void main(String[] a) {
        testCaseInsensitiveMap();
    }
}


2.    identity map:

package usefullmap;

import org.apache.commons.collections.map.IdentityMap;

public class CommonMaps2 {
    public static void testIdentityMap() {
        System.out.println("===== testIdentityMap =====");
        IdentityMap identMap = new IdentityMap();
        Integer identRef = Integer.valueOf(1);
        Integer identRef2 = Integer.valueOf(1);
        Integer identRef3 = Integer.valueOf(3);
        identMap.put(identRef, "value1");
        identMap.put(identRef2, "value2");
        identMap.put(identRef3, "value3");

        System.err.println("==================: " + identMap); // value 2 even though both identRef and identRef2 are equal
        System.err.println("Value of identRef2: " + identMap.get(identRef2)); // value 2 even though both identRef and identRef2 are equal
    }

    public static void main(String[] a) {
        testIdentityMap();
    }
}

3.    bi-direction map:

package usefullmap;

import org.apache.commons.collections.BidiMap;
import org.apache.commons.collections.bidimap.DualHashBidiMap;

public class CommonMaps3 {
    public static void testBidiMap() {
        System.out.println("===== testBidiMap =====");
        BidiMap bidimap = new DualHashBidiMap();
        bidimap.put("Shanghai", "Shanghai");
        bidimap.put("Anhui", "Hefei");
        bidimap.put("JiangSu", "Nanjing");

        System.err.println("Anhui Province capital city is " + bidimap.get("Anhui"));
        System.err.println("Capital City Hefei is in Province " + bidimap.getKey("Hefei"));
    }
    
    public static void main(String[] a) {
        testBidiMap();
    }
}

In order to run the code successfully, you need to put the jakarta commons collection jar file in your class path for compilation and running. The following is the result:

===== testCaseInsensitiveMap =====
==================: {key1=value3, key2=value2}
Value of KEY1: value3

===== testIdentityMap =====
==================: {1=value2, 3=value3}
Value of identRef2: value2

===== testBidiMap =====
Anhui Province capital city is Hefei
Capital City Hefei is in Province Anhui



版权声明  
本篇文章对您是否有帮助?  投票:         投票结果:     8       0
作者其它文章: 作者全部文章
评论人:mrou2001 发表时间: Thu Dec 07 14:27:12 CST 2006
加油啊,支持[java]
评论人:westlifesz 发表时间: Sat Dec 09 19:37:40 CST 2006
very good!

这个文章共有 2 条评论
主题: Java 5 中的 JSR-223(Scripting for the Java Platform) 实现. 上一篇文章
返回文章列表 返回〔J2SE综合〕
下一篇文章 主题: Java调用可执行文件和批处理命令


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

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

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