Reva Tech: Software Solution
Reva Tech: Software Solution
MAP
WHAT IS MAP
Ex 1:
import java.util.HashMap;
import java.util.Map;
// Creating a HashMap
numberMapping.put("One", 1);
numberMapping.put("Two", 2);
numberMapping.put("Three", 3);
// Add a new key-value pair only if the key does not exist in the HashMap, or is
mapped to `null`
numberMapping.putIfAbsent("Four", 4);
System.out.println(numberMapping);
HashMap:
A HashMap contains values based on the key.
It contains only unique elements.
It may have one null key and multiple null values.
It maintains no order
TreeMap:
A TreeMap contains values based on the key. It implements the NavigableMap
interface and extends AbstractMap class.
It contains only unique elements.
It cannot have null key but can have multiple null values.
It is same as HashMap instead maintains ascending order(Sorted using the
natural order of its key).
Hashtable:
A Hashtable is an array of list. Each list is known as a bucket. The position of
bucket is identified by calling the hashcode() method. A Hashtable contains
values based on the key.
It contains only unique elements.
It may have not have any null key or value.
It is synchronized.
It is a legacy class.