Map in Java

Not Geo map

Hash Map

Available Methods

.put()
.get()
.containsKey()
.containsValue()
.getOrDefault()
.forEach() // to iterate over
.keySet()  // for all keys
.values()  // for all values
.replace()
.replaceAll((key,value) -> value+10)
.putIfAbsent()
.remove()

Declaring Hash Map

Map<KeyType, ValueType> myMap = new HashMap<KeyType, ValueType>();

Adding values

Getting values from Hash Map

Check if keys exists

get Or Default

Iterating over hashMap

ONLY KEYS

ONLY VALUES

replace

If the key is present then the value is replaced by new-value. If the key is not present, does nothing

replace All

put If Absent

remove

Linked Hash Map

LinkedHashMap class is Hash table and Linked list implementation of the Map interface, with predictable iteration order. It inherits HashMap class and implements the Map interface.

The important points about Java LinkedHashMap class are: A LinkedHashMap contains values based on the key. It contains only unique elements. It may have one null key and multiple null values. It is same as HashMap instead maintains insertion order.

Weakhashmap

Weak References : The objects that are referenced only by weak references are garbage collected eagerly; the GC won’t wait until it needs memory in that case.

Sorted Map

// not that useful

Key points :

  • Sorted Map interface extends Map.

  • Entries are maintained in an ascending key order.

Methods of sorted Map :

Hashtable

// not that useful

Last updated

Was this helpful?