// collectiontopics/MapOps.java // (c)2020 MindView LLC: see Copyright.txt // We make no guarantees that this code is fit for any purpose. // Visit http://OnJava8.com for more book information. // Things you can do with Maps import java.util.concurrent.*; import java.util.*; import onjava.*; public class MapOps { public static void printKeys(Map map) { System.out.print("Size = " + map.size() + ", "); System.out.print("Keys: "); // Produce a Set of the keys: System.out.println(map.keySet()); } public static void test(Map map) { System.out.println( map.getClass().getSimpleName()); map.putAll(new CountMap(25)); // Map has 'Set' behavior for keys: map.putAll(new CountMap(25)); printKeys(map); // Producing a Collection of the values: System.out.print("Values: "); System.out.println(map.values()); System.out.println(map); System.out.println("map.containsKey(11): " + map.containsKey(11)); System.out.println( "map.get(11): " + map.get(11)); System.out.println("map.containsValue(\"F0\"): " + map.containsValue("F0")); Integer key = map.keySet().iterator().next(); System.out.println("First key in map: " + key); map.remove(key); printKeys(map); map.clear(); System.out.println( "map.isEmpty(): " + map.isEmpty()); map.putAll(new CountMap(25)); // Operations on the Set change the Map: map.keySet().removeAll(map.keySet()); System.out.println( "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, 16, 17, 18, 19, 20, 21, 22, 23, 24] Values: [A0, B0, C0, D0, E0, F0, G0, H0, I0, J0, K0, L0, M0, N0, O0, P0, Q0, R0, S0, T0, U0, V0, W0, X0, 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, 16=Q0, 17=R0, 18=S0, 19=T0, 20=U0, 21=V0, 22=W0, 23=X0, 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, 16, 17, 18, 19, 20, 21, 22, 23, 24] map.isEmpty(): true map.isEmpty(): true ... */