MapHeaven 1 : Collection « Apache Common « Java






MapHeaven 1

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;
  private IdentityMap identMap;
  private Map lazyMap;
}
           
       








ApacheCommonMapHeavenV1.zip( 514 k)

Related examples in the same category

1.Collection Bag
2.Transformer Example
3.Collection BidiMap
4.Collection Buffer
5.Collection Closure
6.Comparator Example For BuildIn Data Type
7.Comparator Example For User Defined Class
8.Cookie Bag 2
9.Factory Example 1
10.HashMap Example 1
11.List Example 1
12.Multi Key Example 1
13.MultiKey Example 2
14.Set Example 1
15.Set Example 2
16.Bean Comparator ( Sorting based on Properties of class )Bean Comparator ( Sorting based on Properties of class )