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.
- package usefullmap;
-
- import java.util.HashMap;
- import java.util.Map;
-
- import org.apache.commons.collections.map.FixedSizeMap;
-
- public class CommonMaps4 {
- public static void testFixedSizeMap() {
- System.out.println("===== testFixedSizeMap =====");
- HashMap map = new HashMap();
- map.put("1", "One");
- map.put("2", "Two");
- map.put("3", "Three");
-
- Map fsm = FixedSizeMap.decorate(map);
-
- System.err.println(""+fsm);
- fsm.put("1", "Uno");
- System.err.println(""+fsm);
-
- try {
- fsm.put("4", "Four");
- }
- catch (Exception e) {
- System.err.println("Ha! You can NOT put any more!!");
- e.printStackTrace();
- }
-
- try {
- fsm.remove("3");
- }
- catch (Exception e) {
- System.err.println("Ha Ha! You can NOT remove key either!");
- e.printStackTrace();
- }
- }
-
- public static void main(String[] a) throws Exception {
- testFixedSizeMap();
- }
- }
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.
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:
- package usefullmap;
-
- import java.util.HashMap;
- import java.util.Map;
-
- import org.apache.commons.collections.Predicate;
- import org.apache.commons.collections.PredicateUtils;
- import org.apache.commons.collections.map.PredicatedMap;
-
- public class CommonMaps6 {
- public static void testPredicatedMap() {
- System.out.println("===== testPredicatedMap =====");
- HashMap map = new HashMap();
- map.put("1", "One");
- map.put("2", "Two");
- map.put("3", "Three");
-
- Predicate isStringPredicate = new Predicate() {
- public boolean evaluate(Object object) {
- if (object == null)
- return true;
- if (object.getClass() == String.class)
- return true;
- return false;
- }
- };
-
- Map pm = PredicatedMap.decorate(map,//origin map
- PredicateUtils.notNullPredicate(), //key predicate
- isStringPredicate //value predicate
- );
-
- System.err.println(""+pm);
- pm.put("4", "Four");
- pm.put("null", null);
- System.err.println(""+pm);
-
- try {
- pm.put("5", new Integer(5)); //try to insert integer as value
- }
- catch (IllegalArgumentException e) {
- System.err.println("Ha! You can NOT put integer as value any more!!");
- e.printStackTrace();
- }
- }
-
- public static void main(String[] a) throws Exception {
- testPredicatedMap();
- }
- }
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)
|
|