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
|
|