You are on page 1of 6

Java.util.

Map
 It is an interface which is defined in java.util package.
 It is used to store multiple objects by providing one unique
identification for every object.
 In map all he objects are stored in the form of key and value
pairs.
 In map both key and values are upcasted to java.lang.Obect
type.
EXAMPLE:

NOTE:
 Every key and value added in a map is known as entry.
 Key should be unique but value can be repeated.
METHODS OF MAP INTERFACE:
ACTION METHOD NAME
ADD put(key,value)
putAll(Map m)
REMOVE remove(key)
clear()
ACCESS get(key)
values()
keyset()
entrySet()
SEARCH containsKey(key)
containsValue(value)
MISCELLANEOUS isEmpty()
equals(Object o)
hashCode()
Size()

MAP HIERARCHY:

Java.util.HashMap
 It is an interface which is defined in java.util package.
Constructors:
HashMap()->Creates an empty HashMap objects with an initial
capacity 16.
HashMap(Map c) ->Creates HashMap object and initialize with
the entries from the specified Map.
HashMap(int initialCapacity)-> Creates an empty HashMap
object with the specified capacity
Characteristics:
 Only one null is accepted as a key
 Insertion order is not maintained
 Duplicates keys are not allowed (If it is used the the existing
value of a key is replaced)
 Heterogeneous keys are accepted
 Indexing is not possible
EXAMPLE :

OUTPUT:
Java.util.LinkedHashMap
 It is an interface which is defined in java.util package.
Constructors:
LinkedHashMap()->Creates an empty LinkedHashMap objects
with an initial capacity 16.
LinkedHashMap(Map c) ->Creates LinkedHashMap object and
initialize with the entries from the specified Map.
LinkedHashMap(int initialCapacity)-> Creates an empty
LinkedHashMap object with the specified capacity
Characteristics:
 Only one null is accepted as a key
 Insertion order is maintained
 Duplicates keys are not allowed (If it is used the the existing
value of a key is replaced)
 Heterogeneous keys are accepted
 Indexing is not possible.
EXAMPLE:
OUTPUT:

Java.util.TreeMap
 It is an interface which is defined in java.util package.
 In TreeMap all the entries are by default sorted based on the
keys.
NOTE:
All the key should be of java.lang.Comparable type.
Constructors:
TreeMap()->Creates an empty TreeMap objects.
TreeMap(Map c) ->Creates TreeMap object and initialize with
the entries from the specified Map.
TreeMap(Comparator c)-> Creates an empty TreeMap and all
the entries are sorted based on the specified Comparator.
Characteristics:
 null is not allowed as a key
 Insertion order is maintained (by default all the entries are
sorted)
 Duplicates keys are not allowed (If it is used the existing value
of a key is replaced)
 Heterogeneous keys are not accepted
 Indexing is not possible.
EXAMPLE:

OUTPUT:

SYNTAX TO CREATE GENERIC COLLECTION:


ClassName<KeyType,ValueType> var = new Constractor<K,V>();

You might also like