Friday, January 16, 2009

Case Insensitive Map : The keys, a String, are case insensitive.

The code...
Map aMap = new CaseInsensitiveMap();
aMap.put("A", "The letter A or a");
aMap.put("b", "The letter B or b");
System.out.println("aMap.get(\"a\") = " + aMap.get("a"));
System.out.println("aMap.get(\"A\") = " + aMap.get("A"));
System.out.println("aMap.get(\"b\") = " + aMap.get("b"));
System.out.println("aMap.get(\"B\") = " + aMap.get("B"));

The results..
aMap.get("a") = The letter A or a
aMap.get("A") = The letter A or a
aMap.get("b") = The letter B or b
aMap.get("B") = The letter B or b
Author: Philip A Senger

Iterable Map Interface : Iterate over a map with keys and values.

Just about every map in the commons collection implements this interface. It allows you to iterate over the key and values with a MapIterator. I know that Java 1.5 has features that allow map iteration, but if you are 1.4... this is nice.

MapIterator it = aMap.mapIterator();
while (it.hasNext()) {
   key = it.next();
   value = it.getValue();
   out.println(key + " -> " + value);
}
Author: Philip A Senger