What is a Map?
The Map Interface
Types of Maps, Including:
- Hashmap
- Treemap
- Linked Hashmaps
Collection
?Collection
is an interface which ensures a class has the ability to hold a series of objects.Map
to be a Collection
, however it does not implement the Collection
interface.Map.Entry
.Map.Entry
has a key
and value
which can be retrieved by getKey()
, and getValue()
Iterable
; Values can be iterated over by retrieving the key-set from calling keySet()
.put(key, value)
get(key)
Set
, retrievable with keySet()
Map
interfaceSet
of Map.Entry
allows access to each key
and value
independently.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();
}
ValueType put(KeyType, ValueType)
Map
with a new Entry
of the respective values.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]
ValueType putAll(Map<KeyType, ValueType>)
Map
with a Entry
from the respective Map
.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]
ValueType get(KeyType)
Entry
with a respective key.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
ValueType replace(KeyType, ValueType)
Entry
for the specified key, if currently mapped to the specified value.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
ValueType remove(Object)
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
void containsKey(Object)
Map
contains a mapping for specified key.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
void containsValue(Object)
Map
contains a mapping for specified value.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
int size()
Map
.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
boolean isEmpty()
true
if the size of the Map
is 0
, else returns false
.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
Set<Entry<KeyType, ValueType>> entrySet()
Set
view of the mappings in the Map
.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]
Set<KeyType> keySet()
Set
view of the keys contained in this mappublic 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]
void clear()
Map
.public void demo() {
Map<String, Integer> map = new HashMap<>();
map.put("One", 1);
map.clear();
System.out.println(map.isEmpty()); // prints true
}
Output
true
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 |
|
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]
public void demo() {
Map<String, Integer> map = new TreeMap<>();
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]
public void demo() {
Map<String, Integer> map = new LinkedHashMap<>();
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]