71 lines
2.4 KiB
Java
Raw Normal View History

2015-12-15 11:47:04 -08:00
// collectionsindepth/Maps.java
// (c)2016 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
2016-01-25 18:05:55 -08:00
// Things you can do with Maps
2015-06-15 17:47:35 -07:00
import java.util.concurrent.*;
import java.util.*;
import onjava.*;
2015-06-15 17:47:35 -07:00
public class Maps {
public static void printKeys(Map<Integer,String> map) {
2015-11-03 12:00:44 -08:00
System.out.print("Size = " + map.size() + ", ");
System.out.print("Keys: ");
2015-12-02 09:20:27 -08:00
// Produce a Set of the keys:
System.out.println(map.keySet());
2015-06-15 17:47:35 -07:00
}
public static void test(Map<Integer,String> map) {
2015-11-03 12:00:44 -08:00
System.out.println(map.getClass().getSimpleName());
2016-07-05 14:46:09 -06:00
map.putAll(new CountMap(25));
2015-06-15 17:47:35 -07:00
// Map has 'Set' behavior for keys:
2016-07-05 14:46:09 -06:00
map.putAll(new CountMap(25));
2015-06-15 17:47:35 -07:00
printKeys(map);
// Producing a Collection of the values:
2015-11-03 12:00:44 -08:00
System.out.print("Values: ");
System.out.println(map.values());
System.out.println(map);
2015-12-02 09:20:27 -08:00
System.out.println(
"map.containsKey(11): " + map.containsKey(11));
2015-11-03 12:00:44 -08:00
System.out.println("map.get(11): " + map.get(11));
System.out.println("map.containsValue(\"F0\"): "
2015-06-15 17:47:35 -07:00
+ map.containsValue("F0"));
Integer key = map.keySet().iterator().next();
2015-11-03 12:00:44 -08:00
System.out.println("First key in map: " + key);
2015-06-15 17:47:35 -07:00
map.remove(key);
printKeys(map);
map.clear();
2015-11-03 12:00:44 -08:00
System.out.println("map.isEmpty(): " + map.isEmpty());
2016-07-05 14:46:09 -06:00
map.putAll(new CountMap(25));
2015-06-15 17:47:35 -07:00
// Operations on the Set change the Map:
map.keySet().removeAll(map.keySet());
2015-11-03 12:00:44 -08:00
System.out.println("map.isEmpty(): " + map.isEmpty());
2015-06-15 17:47:35 -07:00
}
public static void main(String[] args) {
test(new HashMap<>());
test(new TreeMap<>());
test(new LinkedHashMap<>());
test(new IdentityHashMap<>());
test(new ConcurrentHashMap<>());
test(new WeakHashMap<>());
}
2015-09-07 11:44:36 -06:00
}
/* Output: (First 11 Lines)
2015-06-15 17:47:35 -07:00
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
...
2015-09-07 11:44:36 -06:00
*/