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

Map Interface

Uploaded by

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

Map Interface

Uploaded by

roshan.prajapati
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

Map interface

What is Map interface and Working of Map


• The Map interface of the Java collections framework provides
the functionality of the map data structure.
• In Java, elements of Map are stored in key/value pairs. Keys
are unique values associated with individual Values.
• A map cannot contain duplicate keys And each key is
associated with a single value.
Working of Map
• We can access and modify values using the keys associated with
them.
• In the above diagram, we have values: United States, Brazil, and
Spain. And we have corresponding keys: us, br, and es.
• Now, we can access those values using their corresponding keys.
• Note: The Map interface maintains
3 different sets:
the set of keys
the set of values
the set of key/value associations (mapping).
How to use Map?
• In Java, we must import the java.util.Map package in order to use Map.
Once we import the package, here's how we can create a map.
• // Map implementation using HashMap
Map<Key, Value> numbers = new HashMap<>();
• In the above code, we have created a Map named numbers. We have
used the HashMap class to implement the Map interface.
Here,
• Key - a unique identifier used to associate each element (value) in a
map
• Value - elements associated by keys in a map
HashMap
• The HashMap class of the Java collections framework
provides the functionality of the hash table data structure.

• It stores elements in key/value pairs. Here, keys are unique


identifiers used to associate each value on a map.

• The HashMap class implements the


Map interface.
Create a HashMap
• In order to create a hash map, we must import the java.util.HashMap
package first. Once we import the package, here is how we can create
hashmaps in Java.

• The default capacity will be 16 and the default load factor will be 0.75.
HashMap<K, V> numbers = new HashMap<>();

• In the above code, we have created a hashmap named numbers. Here, K


represents the key type and V represents the type of values. For example,

HashMap<String, Integer> numbers = new HashMap<>();


• Here, the type of keys is String and the type of values is Integer.
Basic Operations on Java HashMap

• The HashMap class provides various methods to perform


different operations on hashmaps. We will look at some
commonly used arraylist operations in this tutorial:
• Add elements
• Access elements
• Change elements
• Remove elements

For Program Refer Notepad


Java Hashtable class
• Java Hashtable class implements a hashtable, which maps keys to
values. It inherits Dictionary class and implements the Map
interface.
public class Hashtable<K,V> extends Dictionary<K,V> implements Map<K,V>,
Cloneable, Serializable
• 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.
• The initial default capacity of Hashtable class is 11 whereas
loadFactor is 0.75.
Why u need other data structures and hashing ?
• Now the question arises if Array was already there, what was the
need for a new data structure! The answer to this is in the word
“efficiency“.
• Though storing in Array takes O(1) time, searching in it takes at
least O(log n) time.
• This time appears to be small, but for a large data set, it can cause a
lot of problems and this, in turn, makes the Array data structure
inefficient.
• So now we are looking for a data structure that can store the data
and search in it in constant time, i.e. in O(1) time. This is how
Hashing data structure came into play.
Components of Hashing
There are majorly three components of hashing:
1.Key: A Key can be anything string or integer which is fed as input
in the hash function the technique that determines an index or
location for storage of an item in a data structure.
2.Hash Function: The hash function receives the input key and
returns the index of an element in an array called a hash table. The
index is known as the hash index.
3.Hash Table: Hash table is a data structure that maps keys to values
using a special function called a hash function. Hash stores the data
in an associative manner in an array where each data value has its
own unique index.
What is Collision?

• The hashing process generates a small number for a big key, so there is a
possibility that two keys could produce the same value.
• The situation where the newly inserted key maps to an already occupied, and
it must be handled using some collision handling technology.
LinkedHashMap
• The LinkedHashMap class of the Java collections framework provides
the hash table and linked list implementation of the Map interface.

• The LinkedHashMap class extends the HashMap


to store its entries in a hash table.

• It internally maintains a doubly-linked list


among all of its entries to order its entries.
TreeMap
• The TreeMap class of the Java collections framework provides
the tree data structure implementation.
• It implements the NavigableMap interface which extends
SortedMap interface which extends Map interface.
Creating a TreeMap
we must import the java.util.TreeMap package first
TreeMap<Key, Value> numbers = new TreeMap<>();
Methods for Navigation
• Since the TreeMap class implements NavigableMap, it provides
various methods to navigate over the elements of the treemap.
1. First and Last Methods
• firstKey() - returns the first key of the map
• firstEntry() - returns the key/value mapping of the first key of the
map
• lastKey() - returns the last key of the map
• lastEntry() - returns the key/value mapping of the last key of the
map
TreeMap Comparator
• Treemap elements are sorted naturally (in ascending order).
However, we can also customize the ordering of keys.
• For this, we need to create our own comparator class based on

You might also like