Java loop the map

3 Examples to Loop Map in Java — Foreach vs Iterator

There are multiple ways to loop through Map in Java, you can either use a foreach loop or Iterator to traverse Map in Java, but always use either Set of keys or values for iteration. Since Map by default doesn’t guarantee any order, any code which assumes a particular order during iteration will fail. You only want to traverse or loop through a Map, if you want to transform each mapping one by one. Now Java 8 release provides a new way to loop through Map in Java using Stream API and forEach method. For now, we will see 3 ways to loop through each element of Map.

Though Map is an interface in Java, we often loop through common Map implementation like HashMap , Hashtable , TreeMap, and LinkedHashMap . By the way, all the ways of traversing Map discussed in this article is pretty general and valid for any Map implementation, including proprietary and third-party Map classes.

What I mean by looping through Map is getting mappings one at a time, processing them, and moving forward. Let’s say, we have a Map of Workers , after appraisal, every worker has got an 8% hike, now our task is to update this Map, so each worker’s object reflects its new, increased salary.

Читайте также:  Define file path in php

In order to do this task, we need to iterate through Map, get the Employee object, which is stored as the value. Update Worker with a new salary, and move on. Did you ask about saving it back to Map? no need.

When you retrieve values from Map using the get() method, they are not removed from Map, so one reference of the same object is always there in Map until you remove it. That’s why, you just need to update an object, no need to save it back.

By the way, remember to override equals() and hashCode() method for any object, which you are using as key or value in Map. The internal code of HashMap uses both of these methods to insert and retrieve objects into Map, to learn more see How Map works in Java. Now, Let’s see how can we loop through Map in Java.

1. Using for-each loop with Entry Set

The Map provides you a method called entrySet() , which returns a Set of all Map.Entry objects, which are nothing but mapping stored in Map. If you need both key and value to the process, then this is the best way, as you get both key and value wrapped in entry, you don’t need to call the get() method which goes back to Map again to retrieve the value.

Now, enhanced for loop of Java 5 makes your job easy, all you need to do is to iterate through that collection as shown in the below example :

 SetEntry > entrySet = workersById.entrySet(); for (Map.Entry entry : entrySet) < Worker worker = entry.getValue(); System.out.printf("%s :\t %d %n", worker.getName(), worker.getSalary()); >

2. Using Iterator with KeySet

If you don’t require values and only need a key then you can use Map.keySet() method to get a set of all keys. Now you can use Iterator to loop through this Set. If you need value any time, then you need to get (key) method of Map, which returns a value from Map.

This is an additional trip to map, that’s why the previous method is better if you need both. Here is how to loop through Map using iterator and key set in Java :

 Iterator itr = workersById.keySet().iterator(); while (itr.hasNext()) < Worker current = workersById.get(itr.next()); int increasedSalary = (int) (current.getSalary() * 1.08); current.setSalary(increasedSalary); >

3. Using enhanced foreach loop with values

Now, this is the optimal way of looping through Map if you only need values, as it requires less memory than the first approach. Here also we will use an enhanced foreach loop to traverse through values, which is a Collection .

We can’t use plain old for loop with index because Collection doesn’t provide any get(index) method, which is only available in List , and it’s totally unnecessary to convert Collection to List for iteration only. Here is how to loop Map in Java using enhanced for loop and values method :

Collection workers = workersById.values(); System.out.println("New Salaries of Workers : "); for (Worker worker : workers) < System.out.printf("%s :\t %d %n", worker.getName(), worker.getSalary()); >

Complete Java Program to Loop through Map

How to loop a Map in Java with Example

Here is full Java code, which you can run in your Java IDE or by using the command prompt. Just copy this code and paste into a Java source file with the name MapLoopDemo and save it as .java extension, compile it using «javac» and run it using «java» command.

import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Set; /** * Java Program to loop through Map in Java. This examples shows three ways of * traversing through Map, foreach loop, Iterator with set of keys, entries and * values from Map. * * @author Javin Paul */ public class HelloJUnit < private static final Map workersById = new HashMap<>(); public static void main(String args[]) < // let's initialize Map for // useworkersById.put(76832, new Worker(76832, "Tan", 8000)); workersById.put(76833, new Worker(76833, "Tim", 3000)); workersById.put(76834, new Worker(76834, "Tarun", 5000)); workersById.put(76835, new Worker(76835, "Nikolai", 6000)); workersById.put(76836, new Worker(76836, "Roger", 3500)); // looping through map using foreach loop // print initial salary of workers System.out.println("Initial Salaries of workers"); SetEntry > entrySet = workersById.entrySet(); for (Map.Entry entry : entrySet) < Worker worker = entry.getValue(); System.out.printf("%s :\t %d %n", worker.getName(), worker.getSalary()); > // looping through map using Iterator // updating salary of each worker Iterator itr = workersById.keySet().iterator(); while (itr.hasNext()) < Worker current = workersById.get(itr.next()); int increasedSalary = (int) (current.getSalary() * 1.08); current.setSalary(increasedSalary); > // how to loop map using for loop // looping through values using for loop Collection workers = workersById.values(); System.out.println("New Salaries of Workers : "); for (Worker worker : workers) < System.out.printf("%s :\t %d %n", worker.getName(), worker.getSalary()); > > private static class Worker < private int id; private String name; private int salary; public Worker(int id, String name, int salary) < this.id = id; this.name = name; this.salary = salary; > public final int getId() < return id; > public final String getName() < return name; > public final int getSalary() < return salary; > public final void setId(int id) < this.id = id; > public final void setName(String name) < this.name = name; > public final void setSalary(int salary) < this.salary = salary; > @Override public String toString() < return "Worker [id color: #ff7800;">+ id + ", name color: #ff7800;">+ name + ", salary color: #ff7800;">+ salary + "]"; > @Override public int hashCode() < final int prime = 31; int result = 1; result = prime * result + id; result = prime * result + ((name == null) ? 0 : name.hashCode()); result = prime * result + (int) (salary ^ (salary >>> 32)); return result; > @Override public boolean equals(Object obj) < if (this == obj) < return true; > if (obj == null) < return false; > if (getClass() != obj.getClass()) < return false; > Worker other = (Worker) obj; if (id != other.id) < return false; > if (name == null) < if (other.name != null) < return false; > > else if (!name.equals(other.name)) < return false; > if (salary != other.salary) < return false; > return true; > > > Output: Initial Salaries of workers Tim : 3000 Tan : 8000 Nikolai : 6000 Tarun : 5000 Roger : 3500 New Salaries of Workers : Tim : 3240 Tan : 8640 Nikolai : 6480 Tarun : 5400 Roger : 3780

That’s all about how to loop through Map in Java. Traversing is a common task in any programming language and many of them provide easier syntax, contrary to that, In Java, you need to write a lot of boilerplate code and our functional intent is hidden between boilerplate code. Java 8 is coming up with a more expressive way to do the common tasks in the collection using lambda expressions and bulk data operations.

For examples task like looping through a List , filtering certain elements, transforming one object to another, and reducing them into one value will be very easy. So far, you can use the above examples to loop through your Map in Java. All examples are valid for any Map implementations including HashMap , Hashtable , TreeMap, and LinkedHashMap .

  1. Difference between HashMap and Hashtable in Java? (answer)
  2. How do you sort a Map on keys and values in Java? (answer)
  3. Array length vs ArrayList size() ? (read here)
  4. How do you remove() elements from ArrayList in Java? (answer)
  5. How HashSet works internally in Java? (answer)
  6. 10 ways to use ArrayList in Java? (see here)
  7. Difference between Set , List, and Map in Java? (answer)
  8. When to use ArrayList over LinkedList in Java? (answer)
  9. Difference between HashSet and HashMap in Java? (answer)
  10. Difference between HashMap and LinkdHashMap in Java? (answer)
  11. When to use HashSet vs TreeSet in Java? (answer)
  12. How to sort ArrayList in increasing order in Java? (answer)
  13. Best way to loop through ArrayList in Java? (answer)
  14. Difference between ConcurrentHashMap vs HashMap in Java? (answer)

Источник

6 methods how to loop a Map in Java

6 methods how to loop a Map in Java - images/logos/java.jpg

In this article will describe how to loop through a Map object.

  • method 1: is using entrySet method to get the key:value combination, then using an iterator it will loop through using a while loop.
  • method 2: is using keySet method to get the keys, then using a for loop will go through all the keys and will take the values from the map object.
  • method 3: is using entrySet method to get the key:value combination and will use a for loop to go through all key:value object.
  • method 4: is Java 8 specific and is using the forEach method from the map and the lambda to associate a method with it.
  • method 5: is using keySet to get the keys, then using a while loop will go through all the keys, the method is similar to method 2.
  • method 6: is using the values method to get a collection of values then using an iterator will loop through using while loop, also a for loop can be used to loop the iterator object.

Note that different combinations of these 6 methods can be used to loop through different types of collections and just changing the looping method you can get a different method.

Full example

package com.admfactory.basic; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class MapLoopExample < public static void main(String[] args) < System.out.println("Map loop example"); System.out.println(); /** creating a new Map object */ Mapmap = new HashMap(); /** adding some values */ map.put("key1", "value1"); map.put("key2", "value2"); map.put("key3", "value3"); map.put("key4", "value4"); map.put("key5", "value5"); /** using while and interator method */ System.out.println("Method 1: "); Iterator iterator = map.entrySet().iterator(); while (iterator.hasNext()) < Map.Entryentry = (Map.Entry) iterator.next(); System.out.println(entry.getKey() + " ==> " + entry.getValue()); > System.out.println(); /** using for and keySet to get the keys and values */ System.out.println("Method 2:"); for (Object key : map.keySet()) < System.out.println(key.toString() + " ==>" + map.get(key)); > System.out.println(); /** using for and entrySet method */ System.out.println("Method 3: "); for (Map.Entry entry : map.entrySet()) < System.out.println(entry.getKey() + " ==>" + entry.getValue()); > System.out.println(); /** using Java 8, forEach and Lambda */ System.out.println("Method 4:"); map.forEach((k, v) -> System.out.println(k + " ==> " + v)); System.out.println(); System.out.println("Method 5:"); Set set = map.keySet(); Iterator keys = set.iterator(); while (keys.hasNext()) < String key = keys.next(); System.out.println(key + " ==>" + map.get(key)); > System.out.println(); System.out.println("Method 6: "); Collection valuesC = map.values(); Iterator values = valuesC.iterator(); while (values.hasNext()) < String value = values.next(); System.out.println(value); >> > 

Output

Map loop example Method 1: key1 ==> value1 key2 ==> value2 key5 ==> value5 key3 ==> value3 key4 ==> value4 Method 2: key1 ==> value1 key2 ==> value2 key5 ==> value5 key3 ==> value3 key4 ==> value4 Method 3: key1 ==>value1 key2 ==>value2 key5 ==>value5 key3 ==>value3 key4 ==>value4 Method 4: key1 ==> value1 key2 ==> value2 key5 ==> value5 key3 ==> value3 key4 ==> value4 Method 5: key1 ==> value1 key2 ==> value2 key5 ==> value5 key3 ==> value3 key4 ==> value4 Method 6: value1 value2 value5 value3 value4

References

Источник

Оцените статью