Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
6 views13 pages

Java STL III

In Java, a Map is an interface that maps keys to values without allowing duplicate keys, with various types of declarations available. A Multimap can be simulated using a Map where each key maps to a list of values, while a HashMap serves as an unordered map that uses hashing for fast access. The document also includes time complexity analysis for operations like insertion, removal, and search across different map types.

Uploaded by

partesujit007
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)
6 views13 pages

Java STL III

In Java, a Map is an interface that maps keys to values without allowing duplicate keys, with various types of declarations available. A Multimap can be simulated using a Map where each key maps to a list of values, while a HashMap serves as an unordered map that uses hashing for fast access. The document also includes time complexity analysis for operations like insertion, removal, and search across different map types.

Uploaded by

partesujit007
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/ 13

Map in Java

In Java, a Map is an interface that represents a mapping between a key and a value. It
cannot contain duplicate keys, and each key can map to at most one value.

Map<KeyType, ValueType> mapName = new HashMap<>();

Key-Value Pair

In Java, a key-value pair is represented by the

Map.Entry interface. This interface is nested within the Map interface and provides
methods to access keys and values.

Types of Declarations

You can declare a map in Java with various data types as keys and values. Some
common examples include:

●​ Map<Integer, Integer>: Map with integer keys and integer values.


●​ Map<String, String>: Map with string keys and string values.
●​ Map<Integer, List<Integer>>: Map with integer keys and a list of integers as
values.
●​ Map<String, Employee>: Map with string keys and custom object values
●​ Map<Integer, String> map: This line declares a Map called map with Integer
keys and String values.
●​ map.put(key, value): Adds a key-value pair to the map.
●​ map.get(key): Returns the value to which the specified key is mapped, or null
if the map contains no mapping for the key.
●​ map.containsKey(key): Checks if a key is present in the map.

Multimap in Java

●​ In Java, a​
Multimap is not a part of the standard Java Collections Framework.
●​ However, you can simulate a multimap using a​
Map where each key maps to a list of values. This allows multiple values to
be associated with a single key.

Map<KeyType, List<ValueType>> multimap = new HashMap<>();



Map<Integer, List<String>> multimap: This line declares a Map called multimap
with Integer keys and a list of String values.
●​ multimap.put(key, new ArrayList<>()): Initializes the list for the key before
adding values.
●​ multimap.get(key).add(value): Adds a value to the list associated with the
specified key.
●​ multimap.get(key): Retrieves the list of values associated with the specified
key, or null if no mapping for the key exists.

Unordered Map in Java

●​ In Java, the equivalent of an unordered map from C++ is the​


HashMap class. A HashMap stores key-value pairs and uses hashing to
ensure fast access to values based on their keys.
●​ It does not maintain the order of elements and ensures the uniqueness of
keys, with each key mapping to a unique value.

●​ KeyType represents the type of keys used in the map.


●​ ValueType represents the type of values that will be stored.

You can use methods like put, remove, and get (or find) to work with the HashMap.

HashMap<KeyType, ValueType> myMap: Declares a HashMap with specified key
and value types.

●​ myMap.put(key, value): Adds a new key-value pair to the HashMap or


updates the value if the key already exists.
●​ myMap.remove(key): Removes the mapping for the specified key from the
map if present.
●​ myMap.containsKey(key): Checks if the map contains a mapping for the
specified key.
●​ myMap.get(key): Returns the value to which the specified key is mapped, or
null if the map contains no mapping for the key.

Time Complexity Analysis

Operation Map (like MultiMap HashMap (Unordered


TreeMap) Map)

O(log N) or O(1), worst case -


Insertion O(log N)
O(1) O(N)

O(log N) or O(1), worst case -


Removal O(log N)
O(1) O(N)

O(log N) or O(1), worst case -


Search O(log N)
O(1) O(N)

You might also like