Maps, Dictionaries, Associative Arrays

What we’ll cover

What is a Map?

The Map Interface

Types of Maps, Including:

- Hashmap

- Treemap

- Linked Hashmaps

What is a Collection?

What is a Map?

Using Maps

Map.Entry Interface

Map Interface

public interface Map<KeyType, ValueType> {
  void clear();
  boolean containsKey(Object);
  boolean containsValue(Object);
  Set<Map.Entry<KeyType, ValueType>> entrySet();
  ValueType get(Object);
  ValueType getOrDefault(Object);
  boolean isEmpty();
  Set<KeyType> keySet();
  ValueType put(KeyType, ValueType);
  void putAll(Map<KeyType, ValueType>);
  ValueType remove(Object);
  ValueType replace(KeyType, ValueType);
  int size();
  Collection<ValueType> values();
}

Map Interface

public void demo() {
  Map<String, Integer> map = new HashMap<>();
  map.put("One", 1);
  map.put("Two", 2);
  map.put("Three", 3);
  System.out.println(map.entrySet());
}

Output

[One=1, Two=2, Three=3]

Map Interface

public void demo() {
  Map<String, Integer> map = new HashMap<>();
  Map<String, Integer> newMap = new HashMap<>();
  map.put("One", 1);
  map.put("Two", 2);
  map.put("Three", 3);
  newMap.putAll(map);
  System.out.println(newMap.entrySet());
}

Output

[One=1, Two=2, Three=3]

Map Interface

public void demo() {
  Map<String, Integer> map = new HashMap<>();
  map.put("One", 1);
  map.put("Two", 2);
  System.out.println(map.get("Two")); // prints 2
}

Output

2

Map Interface

public void demo() {
  Map<String, Integer> map = new HashMap<>();
  map.put("One", 1);
  map.put("Two", 2);
  map.replace("Two", 3);
  System.out.println(map.get("Two")) // prints 3
}

Output

3

Map Interface

public void demo() {
  Map<String, Integer> map = new HashMap<>();
  map.put("One", 1);
  map.put("Two", 2);
  map.remove("Two");
  System.out.println(map.get("Two")) // prints null
}

Output

null

Map Interface

public void demo() {
  Map<String, Integer> map = new HashMap<>();
  map.put("One", 1);
  map.put("Two", 2);
  System.out.println(map.containsKey("One")); //prints true
}

Output

true

Map Interface

public void demo() {
  Map<String, Integer> map = new HashMap<>();
  map.put("One", 1);
  map.put("Two", 2);
  System.out.println(map.containsValue(1)); //prints true
}

Output

true

Map Interface

public void demo() {
  Map<String, Integer> map = new HashMap<>();
  map.put("One", 1);
  map.put("Two", 2);
  map.put("Three", 3);
  System.out.println(map.size()); //prints 3
}

Output

3

Map Interface

public void demo() {
  Map<String, Integer> map = new HashMap<>();
  System.out.println(map.size()); //prints 0
  System.out.println(map.isEmpty()); //prints true
  map.put("One", 1);
  System.out.println(map.isEmpty()); //prints false
  System.out.println(map.size()); //prints 1
}

Output

0
true
false
1

Map Interface

public void demo() {
  Map<String, Integer> map = new HashMap<>();
  map.put("One", 1);
  map.put("Two", 2);
  map.put("Three", 3);
  System.out.println(map.entrySet());
}

Output

[One=1, Two=2, Three=3]

Map Interface

public void demo() {
  Map<String, Integer> map = new HashMap<>();
  map.put("One", 1);
  map.put("Two", 2);
  map.put("Three", 3);
  System.out.println(map.keySet()); // prints null
}

Output

[One, Two, Three]

Map Interface

public void demo() {
  Map<String, Integer> map = new HashMap<>();
  map.put("One", 1);
  map.clear();
  System.out.println(map.isEmpty()); // prints true
}

Output

true

Maps

|||| | ————— | ———— | | HashMap | Hashes keys for quick access | HashTable | synchronous operations | TreeMap | entries are ordered by key | EnumMap | Has key type of specified enum map | LinkedHashMap | Hashes keys, preserves order with LinkedList | WeakHashMap | Has WeakReference key-types | IdentityHashMap | Uses System.identityHashCode() and == for storage and retrieval ||||

HashMap

HashMap Example

public void demo() {
    Map<String, Integer> map = new HashMap<>();
    map.put("One", 1);
    map.put("Two", 2);
    map.put("Three", 3);
    map.put("Four", 4);
    map.put("Five", 4);
    System.out.println(map.entrySet());
}

Output

[Five=4, One=1, Four=4, Two=2, Three=3]

TreeMap

TreeMap Example

public void demo() {
    Map<String, Integer> map = new HashMap<>();
    map.put("One", 1);
    map.put("Two", 2);
    map.put("Three", 3);
    map.put("Four", 4);
    map.put("Five", 4);
    System.out.println(map.entrySet());
}

Output

[Five=4, Four=4, One=1, Three=3, Two=2]

LinkedHashMap

LinkedHashMap Example

public void demo() {
    Map<String, Integer> map = new HashMap<>();
    map.put("One", 1);
    map.put("Two", 2);
    map.put("Three", 3);
    map.put("Four", 4);
    map.put("Five", 4);
    System.out.println(map.entrySet());
}

Output

[One=1, Two=2, Three=3, Four=4, Five=4]