TreeMap class in Java Collection

The TreeMap Class implements the Map Interface of the java collection framework . The TreeMapstores the element in the tree like structure. TreeMap does not provide synchronization but allows to provide “Null” Keys and “Null” Values .

The TreeMap uses initial capacity and load factor as parameters. The initial capacity determines the number of buckets in the hash table whereas load factor determines how to retrieve hash table before its initial capacity is increased. When the hash table reaches to its full capacity, then it is rehashed, means the internal data structures are re-built and it doubles the buckets in the hash table. The HashMap initial default capacity of is 16 with a load factor of 0.75.

TreeMap Class in Java Collection

The HashSet Interface class Hierarchy is given below

java.lang.Object
-> java.util.AbstractMap<K,V>
->-> java.util.TreeMap<K,V>

The implemented interfaces by HashMap Interface:

Serializable, Cloneable, Map <K,V>

public class HashMap<K,V>  extends AbstractMap<K,V>  implements Map<K,V>, Cloneable, Serializable

The HashSet Interface provides the below given constructors

ConstructorDescription
TreeMap()It is used to construct an empty tree map that will be sorted using the natural order of its key.
TreeMap(Comparator<? super K> comparator)It is used to construct an empty tree-based map that will be sorted using the comparator comp.
TreeMap(Map<? extends K,? extends V> m)It is used to initialize a treemap with the entries from m, which will be sorted using the natural order of the keys.
TreeMap(SortedMap<K,? extends V> m)It is used to initialize a treemap with the entries from the SortedMap sm, which will be sorted in the same order as sm.

Methods of HashMap Interface

TreeMap MethodTreeMap Method Description
Map.Entry<K,V> ceilingEntry(K key)It returns the key-value pair having the least key, greater than or equal to the specified key, or null if there is no such key.
K ceilingKey(K key)It returns the least key, greater than the specified key or null if there is no such key.
void clear()It removes all the key-value pairs from a map.
Object clone()It returns a shallow copy of TreeMap instance.
Comparator<? super K> comparator()It returns the comparator that arranges the key in order, or null if the map uses the natural ordering.
NavigableSet<K> descendingKeySet()It returns a reverse order NavigableSet view of the keys contained in the map.
NavigableMap<K,V> descendingMap()It returns the specified key-value pairs in descending order.
Map.Entry firstEntry()It returns the key-value pair having the least key.
Map.Entry<K,V> floorEntry(K key)It returns the greatest key, less than or equal to the specified key, or null if there is no such key.
void forEach(BiConsumer<? super K,? super V> action)It performs the given action for each entry in the map until all entries have been processed or the action throws an exception.
SortedMap<K,V> headMap(K toKey)It returns the key-value pairs whose keys are strictly less than toKey.
NavigableMap<K,V> headMap(K toKey, boolean inclusive)It returns the key-value pairs whose keys are less than (or equal to if inclusive is true) toKey.
Map.Entry<K,V> higherEntry(K key)It returns the least key strictly greater than the given key, or null if there is no such key.
K higherKey(K key)It is used to return true if this map contains a mapping for the specified key.
Set keySet()It returns the collection of keys exist in the map.
Map.Entry<K,V> lastEntry()It returns the key-value pair having the greatest key, or null if there is no such key.
Map.Entry<K,V> lowerEntry(K key)It returns a key-value mapping associated with the greatest key strictly less than the given key, or null if there is no such key.
K lowerKey(K key)It returns the greatest key strictly less than the given key, or null if there is no such key.
NavigableSet<K> navigableKeySet()It returns a NavigableSet view of the keys contained in this map.
Map.Entry<K,V> pollFirstEntry()It removes and returns a key-value mapping associated with the least key in this map, or null if the map is empty.
Map.Entry<K,V> pollLastEntry()It removes and returns a key-value mapping associated with the greatest key in this map, or null if the map is empty.
V put(K key, V value)It inserts the specified value with the specified key in the map.
void putAll(Map<? extends K,? extends V> map)It is used to copy all the key-value pair from one map to another map.
V replace(K key, V value)It replaces the specified value for a specified key.
boolean replace(K key, V oldValue, V newValue)It replaces the old value with the new value for a specified key.
void replaceAll(BiFunction<? super K,? super V,? extends V> function)It replaces each entry’s value with the result of invoking the given function on that entry until all entries have been processed or the function throws an exception.
NavigableMap<K,V> subMap(K fromKey, boolean fromInclusive, K toKey, boolean toInclusive)It returns key-value pairs whose keys range from fromKey to toKey.
SortedMap<K,V> subMap(K fromKey, K toKey)It returns key-value pairs whose keys range from fromKey, inclusive, to toKey, exclusive.
SortedMap<K,V> tailMap(K fromKey)It returns key-value pairs whose keys are greater than or equal to fromKey.
NavigableMap<K,V> tailMap(K fromKey, boolean inclusive)It returns key-value pairs whose keys are greater than (or equal to, if inclusive is true) fromKey.
boolean containsKey(Object key)It returns true if the map contains a mapping for the specified key.
boolean containsValue(Object value)It returns true if the map maps one or more keys to the specified value.
K firstKey()It is used to return the first (lowest) key currently in this sorted map.
V get(Object key)It is used to return the value to which the map maps the specified key.
K lastKey()It is used to return the last (highest) key currently in the sorted map.
V remove(Object key)It removes the key-value pair of the specified key from the map.
Set<Map.Entry<K,V>> entrySet()It returns a set view of the mappings contained in the map.
int size()It returns the number of key-value pairs exists in the hashtable.
Collection values()It returns a collection view of the values contained in the map.

HashMap Class Tutorial in java collection

The HashMap Class implements the Map Interface of the java collection framework . The HashMap stores the element Key-Value pair in the hash table. HashMap does not provide synchronization but allows to provide “Null” Keys and “Null” Values .

The HashMap uses initial capacity and load factor as parameters. The initial capacity determines the number of buckets in the hash table whereas load factor determines how to retrieve hash table before its initial capacity is increased. When the hash table reaches to its full capacity, then it is rehashed, means the internal data structures are re-built and it doubles the buckets in the hash table. The HashMap initial default capacity of is 16 with a load factor of 0.75.

HashMap Class in Java Collection

The HashMap class Hierarchy is given below

java.lang.Object
-> java.util.AbstractMap<K,V>
->-> java.util.HashMap<K,V>

The implemented interfaces by HashMap Interface:

Serializable, Cloneable, Map <K,V>

public class HashMap<K,V>  extends AbstractMap<K,V>  implements Map<K,V>, Cloneable, Serializable

The HashSet Interface provides the below given constructors

HashMap Constructors HashMapConstructor Description
HashMap()creates a empty HashMap which provides default initial capacity as 16 and load factor as 0.75
HashMap( Map< ? extends K , ? extends V > m )creates a new hash map as per the specified map
HashMap(int initialCapacity)creates a empty HashMap with provided initial capacity and load factor as 0.75
HashMap(int initialCapacity, float loadFactor)creates a empty HashMap with provided initial capacity and provided load factor

Methods of HashMap Interface

HashMap MethodHashMap Method Description
void clear()removes all of the mappings from map.
boolean isEmpty()returns true if map is empty
Object clone()returns a shallow copy of HashMap instance but does not clone the keys and values
Set entrySet()returns a collection view hash map
Set keySet()returns a set view map keys
V put(Object key, Object value)inserts an entry in the map
void putAll(Map map)inserts the specified map in the map
V putIfAbsent(K key, V value)inserts the specified value with the specified key in the map only if it is not already specified
V remove(Object key)removes an entry from the map as per specified key
boolean remove(Object key, Object value)removes specified values with the specified keys from the map
V compute(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)computes mapping for the specified key using the given function where key value is null
V computeIfAbsent(K key, Function<? super K,? extends V> mappingFunction)computes value using the given mapping function, if the specified key is not already associated with a value
V computeIfPresent(K key, BiFunction<? super K,? super V,? extends V> remappingFunction)computes new mapping for the given key and value if key-value pair exists
boolean containsValue(Object value)returns true if given value exists in map
boolean containsKey(Object key)returns true if given key exists in map
boolean equals(Object o)compares the specified Object with the Map
void forEach(BiConsumer<? super K,? super V> action)Executes the specified action for each key-value pair in the map till all have been processed
V get(Object key) returns the object that contains the value associated with the key
V getOrDefault(Object key, V defaultValue)returns value to which the specified key is mapped or map with no key value
boolean isEmpty()returns true if map is empty
V merge(K key, V value, BiFunction<? super V,? super V,? extends V> remappingFunction)If the specified key is not already associated with a value or is null, then associates it with the given value
V replace(K key, V value)replaces given value for a specified key
boolean replace(K key, V oldValue, V newValue)replaces old value with the new value for a specified key
void replaceAll(BiFunction<? super K,? super V,? extends V> function)replaces each entry’s value with the result of invoking the given function on that entry until all have been processed or the function throws an exception.
Collection<V> values()returns a collection view of the map values
int size()returns map size

HashMap Example in Java Collection

package core.java.planforexams;
import java.util.HashMap;
import java.util.Map;
public class BasicHashMapExample {
	public static void main(String args[]){  
	// HashMap Object with student names
	HashMap<Integer,String> students=new HashMap<Integer,String>();//Creating HashMap    
	students.put(1,"Mohit");   
	students.put(2,"Rohit");    
	students.put(3,"Mihika");   
	students.put(4,"Khushal");   
	   System.out.println("Student HashMap Iteration...");  
	   for(Map.Entry names : students.entrySet())
	   {    
		   System.out.println(names.getKey()+" "+names.getValue());    
	   }   
	}
}
Output: 
Student HashMap Iteration...
1 Mohit
2 Rohit
3 Mihika
4 Khushal