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:
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.
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 collection views method allows a map to be viewed as a Collection in the following ways:
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
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.
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.
State: Gujarat State: Sikkim State: Uttar Pradesh Capital: Gandhi Nagar Capital: Ganagtok Capital: Lucknow
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.
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
In the following example we first iterate over the keys and then getting the values.
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.
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.
In the following example, we are using lambda expression inside the forEach() method to print each element of the map.
Company: Wipro, Net worth: $21.5 billion Company: TCS, Net worth: $100 billion
JavaTpoint offers too many high quality services. Mail us on [email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected]
Duration: 1 week to 2 week