Home » How to iterate Map in Java

How to iterate Map in Java

by Online Tutorials Library

How to iterate Map in Java

In Java, iteration over Map can be done in various ways. Remember that we cannot iterate over map directly using iterators, because Map interface is not the part of Collection. All maps in Java implements Map interface. There are following types of maps in Java:

  • HashMap
  • TreeMap
  • LinkedHashMap

A map is not a Collection but still, consider under the Collections framework. Hence, a Map is an interface which does not extend the Collections interface.

Iterator

An iterator is an interface used for iterate over a collection. It takes the place of Enumeration in Java Collections Framework. The difference between iterator and Enumeration is:

  • The Iterator can traverse legacy and non-legacy elements whereas Enumeration can traverse only legacy elements.
  • Iterator is fail-fast whereas Enumeration is not fail-fast.

Collection Views

The collection views method allows a map to be viewed as a Collection in the following ways:

  • keySet: It is the set of keys contained in the Map.
  • values: It is the collection of values contained in the Map.
  • entrySet: It is a set of key-value pair in the Map.

The Map interface also has a small nested interface called Map.entry. The collection view provides the only means to iterate over a map.

Using Iterator interface

Example of iteration over HashMap

Output:

Roll no.: 130     name: Davesh  Roll no.: 150     name: Pawan  Roll no.: 120     name: Prateek  Roll no.: 140     name: Kamal  Roll no.: 110     name: Ravi  

Using keyset() and value() method

keyset(): A keySet() method of HashMap class is used for iteration over the keys contained in the map. It returns the Set view of the keys.

Syntax

values(): A values() method of HashMap class is used for iteration over the values contained in the map. It returns a collection view of the values.

Syntax

Example

Output:

State: Gujarat  State: Sikkim  State: Uttar Pradesh  Capital: Gandhi Nagar  Capital: Ganagtok  Capital: Lucknow  

Using Map.entry<K,V>method

Map.Entry<K,V> is an interface. It returns a collection view of the map, whose elements are of this class.

A map.entrySet() method returns a Set view of the mapping contained in the map. The changes in the map are reflected in the Set also and vice-versa. It also supports element removal, which removes the corresponding mapping from the map.

Syntax

Example

Output:

Item: Oats, Price: 220.0  Item: Dry Fruits, Price: 434.23  Item: Cookies, Price: 90.87  Item: Chocolate, Price: 70.89  

Iteration over keys and getting values

Example

In the following example we first iterate over the keys and then getting the values.

Output:

Key: Rahul, Value: Tiwari  Key: Devesh, Value: Mishra  Key: Sumit, Value: Singh  

Using forEach() method

The forEach() method of ArrayList is used to perform an action for each element of the Iterable until all elements have been processed.

Syntax

The method takes action (the action to be performed for each element) as a parameter. It does not return anything. It throws NullPointerException if the specified action is null.

Example

In the following example, we are using lambda expression inside the forEach() method to print each element of the map.

Output:

Company: Wipro, Net worth: $21.5 billion  Company: TCS, Net worth: $100 billion  

Next TopicJava Tutorial

You may also like