0% found this document useful (0 votes)
4 views

Map Notes

This document discusses Java Map interfaces and classes. It defines Map as an interface that stores objects in key-value pairs, with unique keys. It lists common Map methods like put(), get(), remove(), and describes HashMap, LinkedHashMap and TreeMap classes. HashMap maintains no order, LinkedHashMap preserves insertion order, and TreeMap sorts entries based on their natural ordering.

Uploaded by

Pavan Borra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Map Notes

This document discusses Java Map interfaces and classes. It defines Map as an interface that stores objects in key-value pairs, with unique keys. It lists common Map methods like put(), get(), remove(), and describes HashMap, LinkedHashMap and TreeMap classes. HashMap maintains no order, LinkedHashMap preserves insertion order, and TreeMap sorts entries based on their natural ordering.

Uploaded by

Pavan Borra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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