import java.util.*; public class TestImmutableMap { public static void main(String[] args) { Map ints = new HashMap(); for(int i = 0; i < 3; i++) ints.put(i,""+i); ImmutableMap s1 = new ImmutableMap(ints); System.out.println("s1 = " + s1); System.out.println("s1.size == 3 " + (s1.size() == 3)); ints = new HashMap(); for(int i = 2; i > -1; i--) ints.put(i,""+i); ImmutableMap s2 = new ImmutableMap(ints); System.out.println("s2 = " + s2); System.out.println("s1.equals(s2) = " + s1.equals(s2)); System.out.println("s2.equals(s1) = " + s2.equals(s1)); ints = new HashMap(); for(int i = 3; i > -1; i--) ints.put(i,""+i); ImmutableMap s3 = new ImmutableMap(ints); System.out.println("s3 = " + s3); System.out.println("s1.equals(s3) = " + s1.equals(s3)); System.out.println("s3.equals(s1) = " + s3.equals(s1)); ImmutableMap s4 = new ImmutableMap(s3.keySet().toArray(new Integer[s3.size()]), s3.values().toArray(new String[s3.size()])); System.out.println("s4 = " + s4); System.out.println("s4.equals(s3) = " + s4.equals(s3)); System.out.println("s3.equals(s4) = " + s3.equals(s4)); try { s4.put(new Integer(4),"test"); System.out.println("should not be able to put to s4???"); } catch (UnsupportedOperationException e) { System.out.println(e); } try { s3.clear(); System.out.println("should not be able to clear s3???"); } catch (UnsupportedOperationException e) { System.out.println(e); } System.out.println("s3.containsKey(1) = " + s3.containsKey(1)); System.out.println("s3.containsKey(4) = " + s3.containsKey(4)); System.out.println("s3.containsValue(1) = " + s3.containsValue("1")); System.out.println("s3.containsValue(4) = " + s3.containsValue("4")); System.out.println("s3.entrySet.size() = " + s3.entrySet().size()); System.out.println("s3.size() = " + s3.size()); System.out.println("s3.isEmpty() = " + s3.isEmpty()); System.out.println("s1.hashCode() = " + s1.hashCode()); System.out.println("s2.hashCode() = " + s2.hashCode()); System.out.println("s3.hashCode() = " + s3.hashCode()); System.out.println("s4.hashCode() = " + s4.hashCode()); for(Integer i: s1.keySet()) System.out.println("s1: " + i + " -> " + s1.get(i)); try { s4.remove(0); System.out.println("should not be able to remove from s4???"); } catch (UnsupportedOperationException e) { System.out.println(e); } //System.out.println(Arrays.toString(s3.toArray())); //System.out.println(Arrays.toString(s3.toArray(new Integer[s3.size()]))); try { s3.putAll(s1); System.out.println("should not be able to putAll for s3???"); } catch (UnsupportedOperationException e) { System.out.println(e); } try { s4.putAll(s1); System.out.println("should not be able to putAll for s4???"); } catch (UnsupportedOperationException e) { System.out.println(e); } Map objects = new HashMap(); for(int i = 0; i < 3; i++) { objects.put(String.valueOf(i), ""+i); } ImmutableMap s5 = new ImmutableMap(objects); System.out.println("s5 = " + s5); TreeMap objectMap = new TreeMap(); for(int i = 0; i < 3; i++) { objectMap.put(String.valueOf(i), ""+i); } System.out.println("s5.equals(objectMap) = " + s5.equals(objectMap)); System.out.println("objectMap.equals(s5) = " + objectMap.equals(s5)); ImmutableMap sd = new ImmutableMap(new String[]{"one"}, new Double[]{1.0}); System.out.println("String->Double " + sd); } }