//: containers/Maps.java // ©2015 MindView LLC: see Copyright.txt // Things you can do with Maps. import java.util.concurrent.*; import java.util.*; import net.mindview.util.*; import static net.mindview.util.Print.*; public class Maps { public static void printKeys(Map map) { printnb("Size = " + map.size() + ", "); printnb("Keys: "); print(map.keySet()); // Produce a Set of the keys } public static void test(Map map) { print(map.getClass().getSimpleName()); map.putAll(new CountingMapData(25)); // Map has 'Set' behavior for keys: map.putAll(new CountingMapData(25)); printKeys(map); // Producing a Collection of the values: printnb("Values: "); print(map.values()); print(map); print("map.containsKey(11): " + map.containsKey(11)); print("map.get(11): " + map.get(11)); print("map.containsValue(\"F0\"): " + map.containsValue("F0")); Integer key = map.keySet().iterator().next(); print("First key in map: " + key); map.remove(key); printKeys(map); map.clear(); print("map.isEmpty(): " + map.isEmpty()); map.putAll(new CountingMapData(25)); // Operations on the Set change the Map: map.keySet().removeAll(map.keySet()); print("map.isEmpty(): " + map.isEmpty()); } public static void main(String[] args) { test(new HashMap<>()); test(new TreeMap<>()); test(new LinkedHashMap<>()); test(new IdentityHashMap<>()); test(new ConcurrentHashMap<>()); test(new WeakHashMap<>()); } } /* Output: (First 11 Lines) HashMap Size = 25, Keys: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 16, 19, 18, 21, 20, 23, 22, 24] Values: [A0, B0, C0, D0, E0, F0, G0, H0, I0, J0, K0, L0, M0, N0, O0, P0, R0, Q0, T0, S0, V0, U0, X0, W0, Y0] {0=A0, 1=B0, 2=C0, 3=D0, 4=E0, 5=F0, 6=G0, 7=H0, 8=I0, 9=J0, 10=K0, 11=L0, 12=M0, 13=N0, 14=O0, 15=P0, 17=R0, 16=Q0, 19=T0, 18=S0, 21=V0, 20=U0, 23=X0, 22=W0, 24=Y0} map.containsKey(11): true map.get(11): L0 map.containsValue("F0"): true First key in map: 0 Size = 24, Keys: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 17, 16, 19, 18, 21, 20, 23, 22, 24] map.isEmpty(): true map.isEmpty(): true ... *///:~