0% found this document useful (0 votes)
19 views6 pages

Collection - Framework - Map - by - Kunal - Sir Official

The document provides an overview of the Map interface in Java, explaining its structure as a collection of key-value pairs and its unique characteristics, such as allowing only unique keys. It details the methods associated with the Map interface, including put, get, size, keySet, and entrySet, and compares HashMap, Hashtable, LinkedHashMap, and TreeMap in terms of synchronization, performance, memory consumption, and order of elements. Additionally, it emphasizes that the Map interface is part of the java.util package and outlines how to create a Map object using HashMap.
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)
19 views6 pages

Collection - Framework - Map - by - Kunal - Sir Official

The document provides an overview of the Map interface in Java, explaining its structure as a collection of key-value pairs and its unique characteristics, such as allowing only unique keys. It details the methods associated with the Map interface, including put, get, size, keySet, and entrySet, and compares HashMap, Hashtable, LinkedHashMap, and TreeMap in terms of synchronization, performance, memory consumption, and order of elements. Additionally, it emphasizes that the Map interface is part of the java.util package and outlines how to create a Map object using HashMap.
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

by Kunal Sir

Map Interface in Java:


What is Map?
 A Map in java is a container object that stores elements in
the form of key and value pairs.
 Each key and value pair are known as an Entry.
 The java Map interface is a part of Java Collection
Framework, but it is not a sub-type of the Collection
Interface. So, it behaves in a different way compared to
say, Lists or other collection objects.

Stop, Near, 1st Floor, Above Rupam Sweets/ Priyanka Collections Building Vikas Mitra Mandal Chowk
Road, Karve Nagar, Pune, Maharashtra 411052, Mobile No.- 8888022204
by Kunal Sir

Additional Information Regarding Map:


 Each entry in the map stores the data in a key and its
corresponding value.
 Map interface contains only unique keys and does not
allow any duplicates keys but value duplicate.
 Each key maps to only one value. This type of mapping is
called One-To-One Mapping in java.
 The Map interface in java is a part of the java.util package.
 A key is an object that you use to access the value later, it
is associated with a single value.
 A map cannot be traversed, therefore you must use the
keySet() and entrySet() method to convert it into a set.

Object Creation of Map Interface:


Map<K, V> map = new HashMap<K, V>( );
Where: - Map  Interface.
<K, V>  Entry in Map.
map  Reference variable / object.
new  Keyword.
HashMap  Implemented Class.
()  Parentheses.
K  Key in Map.
V  Value in Map.

Stop, Near, 1st Floor, Above Rupam Sweets/ Priyanka Collections Building Vikas Mitra Mandal Chowk
Road, Karve Nagar, Pune, Maharashtra 411052, Mobile No.- 8888022204
by Kunal Sir

Hierarchy of Map Interface in Java:

Stop, Near, 1st Floor, Above Rupam Sweets/ Priyanka Collections Building Vikas Mitra Mandal Chowk
Road, Karve Nagar, Pune, Maharashtra 411052, Mobile No.- 8888022204
by Kunal Sir

Important Methods of Map Interface:


There are following important methods which is belongs from
the Map Interface is given below-

1. Put Method (put(k, v)): - In map, the given value and key are
associated together using this method.

2. Get Method (get(k)): - The object containing the value


associated with the key is returned by this method.

3. Size Method (size( )): - The number of key/value pairs in the


map is returned by this method.

4. KeySet Method (keySet( )): - It returns Set that contains the


key in a map.

5. EntrySet Method (entrySet( )): - It returns Set that contains


the entries in a map.

Difference between HashMap and Hash


Table: -

There are following difference between HashMap and


HashTable which is given below –

Stop, Near, 1st Floor, Above Rupam Sweets/ Priyanka Collections Building Vikas Mitra Mandal Chowk
Road, Karve Nagar, Pune, Maharashtra 411052, Mobile No.- 8888022204
by Kunal Sir

Hash Map Hash Table

In hash-map, not a single method Every method in hash table is


is synchronized. synchronized.
In hash-map, multiple threads can In hash-table, only one thread is
run at the same time. Hence it is allowed to run at a time. Hence it
not thread-safe. is thread-safe.
In it, thread do not wait, hence On the other hand, threads need
performance is high. to wait which makes the
performance low.
Hash-Map does allow the storage Hash-Table does not allow null
of the null values for both key and values for both key and value
values. because it will give null pointer
exceptions.

Introduced in jdk1.2 version. Introduced in jdk1.0 version.


It is a non-legacy class. It is a legacy class.

Difference between HashMap and


LinkedHashMap: -
There are following difference between HashMap and
LinkedHashMap which is given below –

Hash Map Linked Hash Map

Stop, Near, 1st Floor, Above Rupam Sweets/ Priyanka Collections Building Vikas Mitra Mandal Chowk
Road, Karve Nagar, Pune, Maharashtra 411052, Mobile No.- 8888022204
by Kunal Sir

Hash Map implements Map Linked Hash Map extends the


interface. HashMap class.

HashMap does not guarantee any LinkedHashMap is based on


order. insertion order.
Hash Map takes less memory. Linked HashMap consumes
relatively more memory.

Difference between HashMap and Hash


TreeMap: -
There are following difference between HashMap and TreeMap
which is given below –

Hash Map Tree Map

It does not provide any order for It provides orders for elements.
elements.
It allows one key as null and also It does not allow key as null but it
allows multiple null values. allows multiple null values.
It consumes more memory space. It consumes less memory space.

It’s speed is fast. I It’s speed is low.

Stop, Near, 1st Floor, Above Rupam Sweets/ Priyanka Collections Building Vikas Mitra Mandal Chowk
Road, Karve Nagar, Pune, Maharashtra 411052, Mobile No.- 8888022204

You might also like