阅读更多
Apache-collection中BidiMap、MultiMap和LazyMap的使用
1. import java.util.ArrayList;
2. import java.util.Collection;
3. import java.util.Date;
4. import java.util.HashMap;
5. import java.util.Iterator;
6. import java.util.List;
7. import java.util.Map;
8.
9. import org.apache.commons.collections.BidiMap;
10. import org.apache.commons.collections.Factory;
11. import org.apache.commons.collections.MultiHashMap;
12. import org.apache.commons.collections.MultiMap;
13. import org.apache.commons.collections.bidimap.DualHashBidiMap;
14. import org.apache.commons.collections.map.LazyMap;
15. import org.apache.commons.lang.StringUtils;
16.
17. public class MutiMap
18. {
19.
20. public static void main(String[] args) {
21. //demoBidiMap();
22. //demoMultiMap();
23. demoLazyMap();
24. }
/****
* 所谓BidiMap,直译就是双向Map,可以通过key找到value,
* 也可以通过value找到key,这在我们日常的代码-名称匹配的时候很方便:
* 因为我们除了需要通过代码找到名称之外,往往也需要处理用户输入的名称,然后获取其代码。
* 需要注意的是BidiMap当中不光key不能重复,value也不可以。
*/
1. public static void demoBidiMap() {
2. System.out.println(StringUtils.center(" demoBidiMap ", 40, "="));
3. BidiMap bidiMap = new DualHashBidiMap();
4. bidiMap.put("BJ", "Beijing");
5. bidiMap.put("SH", "Shanghai");
6. bidiMap.put("GZ", "Guangzhou");
7. bidiMap.put("CD", "Chengdu");
8. System.out.println("Key-Value: BJ = " + bidiMap.get("BJ"));
9. System.out.println("Value-Key: Chengdu = " + bidiMap.getKey("Chengdu"));
10. System.out.println(StringUtils.repeat("=", 40));
11. }
/****
* 所谓MultiMap,就是说一个key不在是简单的指向一个对象,而是一组对象,
* add()和remove()的时候跟普通的Map无异,只是在get()时返回一个Collection,
* 利用MultiMap,我们就可以很方便的往一个key上放数量不定的对象,也就实现了一对多。
*/
@SuppressWarnings(value = {"unchecked"})
public static void demoMultiMap()
{
System.out.println(StringUtils.center(" demoMultiMap ", 40, "="));
List list = new ArrayList();
List valuelist = new ArrayList();
list.add("123");
list.add("456");
list.add("789");
MultiMap multiMap = new MultiHashMap();
multiMap.put("Sean", list);
multiMap.put("Sean", "C/C++");
multiMap.put("Sean", "OO");
multiMap.put("Sean", "Java");
multiMap.put("Sean", ".NET");
multiMap.remove("Sean", "C/C++");
System.out.println("Sean's skill set: " + multiMap.get("Sean"));
Iterator itet = ((Collection) multiMap.get("Sean")).iterator();
while(itet.hasNext())
{
Object obj = itet.next();
if(obj instanceof List)
{
valuelist = (List)obj;
for(Object value:valuelist)
{
System.out.println("obj1:"+value);
}
}else if(obj instanceof String)
{
System.out.println("value:"+obj.toString());
}
}
System.out.println(StringUtils.repeat("=", 40));
}
/**所谓LazyMap,意思就是这个Map中的键/值对一开始并不存在,当被调用到时才创建.
* 我们这样来理解:我们需要一个Map,但是由于创建成员的方法很“重”(比如数据库访问),
* 或者我们只有在调用get()时才知道如何创建,或者Map中出现的可能性很多很多,
* 我们无法在get()之前添加所有可能出现的键/值对,
* 我们觉得没有必要去初始化一个Map而又希望它可以在必要时自动处理数据
*
*/
@SuppressWarnings(value = {"unchecked"})
public static void demoLazyMap()
{
System.out.println(StringUtils.center(" demoLazyMap ", 40, "="));
Factory factory = new Factory() {
public Object create() {
return new Date();
}
};
Map lazy = LazyMap.decorate(new HashMap(), factory);
System.out.println("map:"+lazy);//lazy为空
System.out.println(lazy.get("123"));
System.out.println(lazy.get("345"));
System.out.println(StringUtils.repeat("=", 40));
}
}
import java.util.Map;
import java.util.Date;
import java.util.HashMap;
import org.apache.commons.collections.map.LazyMap;
import org.apache.commons.collections.FactoryUtils;
import org.apache.commons.collections.map.IdentityMap;
import org.apache.commons.collections.map.CaseInsensitiveMap;
public class MapHeavenV1 {
public static void main(String args[]) {
MapHeavenV1 instance = new MapHeavenV1();
instance.createMaps();
instance.testMaps();
}
private void testMaps() {
cIMap.put("key1", "value1");
cIMap.put("key2", "value2");
cIMap.put("KeY1", "value3");
System.err.println("Value of key1: " + cIMap.get("key1")); // value3 because it is case insensitive
Integer identRef = new Integer(1);
Integer identRef2 = new Integer(1);
identMap.put(identRef, "value1");
identMap.put(identRef2, "value3");
System.err.println("Value of identRef2: " + identMap.get(identRef2)); // value 3 even though both identRef and identRef2 are equal
System.err.println(lazyMap); // only creates elements when they are accessed
lazyMap.get("EmptyBuffer");
System.err.println(lazyMap);
}
private void createMaps() {
cIMap = new CaseInsensitiveMap();
identMap = new IdentityMap();
lazyMap = LazyMap.decorate(
new HashMap(),
FactoryUtils.instantiateFactory(StringBuffer.class));
}
private CaseInsensitiveMap cIMap;//key大小写不敏感
private IdentityMap identMap;//key数字唯一
private Map lazyMap;
}
MultiKey示例
import java.util.HashMap;
import org.apache.commons.collections.keyvalue.MultiKey;
public class MultiKeyExampleV2 {
private static HashMap codeAndLangToText;
public static void main(String args[]) {
codeAndLangToText = new HashMap();
addMultiKeyAndValue("en", "GM", "Good Morning");
addMultiKeyAndValue("en", "GE", "Good Evening");
addMultiKeyAndValue("en", "GN", "Good Night");
addMultiKeyAndValue("de", "GM", "Guten Morgen");
addMultiKeyAndValue("de", "GE", "Guten Abend");
addMultiKeyAndValue("de", "GN", "Guten Nacht");
System.err.println("Good Evening in English: " +
codeAndLangToText.get(new MultiKey("en", "GE")));
System.err.println("Good Night in German: " +
codeAndLangToText.get(new MultiKey("de", "GN")));
}
private static void addMultiKeyAndValue(
Object key1, Object key2, Object value) {
MultiKey key = new MultiKey(key1, key2);
codeAndLangToText.put(key, value);
}
}