Map Interface in Java

Properties: A map contains values on the basis of key, i.e. key and value pair. Each key and value pair is known as an entry. A Map contains unique keys.

A Map doesn't allow duplicate keys, but you can have duplicate values. HashMap and LinkedHashMap allow null keys and values, but TreeMap doesn't allow any null key or value.

A Map can't be traversed, so you need to convert it into Set using keySet() or entrySet() method.


HashMap Class

Properties: 
Java HashMap class contains values based on the key.
Java HashMap class contains only unique keys.
Java HashMap class may have one null key and multiple null values.
Java HashMap class is non synchronized.
Java HashMap class maintains no order.

Methods: void clear(), Object clone(), boolean containsKey(Object key), boolean containsValue(Object Value) , Object get(Object key), boolean isEmpty(), Set keySet(), Object put(Key k, Value v), int size(), Collection values(), Value remove(Object key)


LinkedHashMap Class

Properties: 
Java LinkedHashMap contains values based on the key.
Java LinkedHashMap contains unique elements.
Java LinkedHashMap may have one null key and multiple null values.
Java LinkedHashMap is non synchronized.
Java LinkedHashMap maintains insertion order.

Methods: void clear(), void size(), void isEmpty(), boolean containsKey(Object key), boolean containsValue(Object key), Object get(Object key)


TreeMap Class

Properties: 
Java TreeMap contains values based on the key. It implements the NavigableMap interface and extends AbstractMap class.
Java TreeMap contains only unique elements.
Java TreeMap cannot have a null key but can have multiple null values.
Java TreeMap is non synchronized.
Java TreeMap maintains ascending order.

Methods: void clear(), void size(), void isEmpty(), boolean containsKey(Object key), boolean containsValue(Object key), Object get(Object key), Object firstKey(), Object lastKey()


HashTable Class:

Properties: Hashtable internally contains buckets in which it stores the key/value pairs. The Hashtable uses the key’s hashcode to determine to which bucket the key/value pair should map.
A Hashtable is an array of a list. Each list is known as a bucket. The position of the bucket is identified by calling the hashcode() method. A Hashtable contains values based on the key.
Java Hashtable class contains unique elements.
Java Hashtable class doesn't allow null key or value.
Java Hashtable class is synchronized.

Methods: void clear(), void size(), void isEmpty(), boolean containsKey(Object key), boolean containsValue(Object key), Object get(Object key)

To get more details please watch below youtube video and Subscribe the channel.




No comments:

Post a Comment