Performing Iteration over each entry in a Map

0 votes
If I have an object implementing the Map interface in Java and I wish to iterate over every pair contained within it, what is the most efficient way of going through the map?

Will the ordering of elements depend on the specific map implementation that I have for the interface?
Apr 17, 2018 in Java by Parth
• 4,630 points
797 views

2 answers to this question.

0 votes

Typical code for iterating over a map is:

Map<String,Thing> map = ...;
for (Map.Entry<String,Thing> entry : map.entrySet()) {
    String key = entry.getKey();
    Thing thing = entry.getValue();
    ...
}

HashMap is the canonical map implementation and doesn't make guarantees (or though it should not change order if no mutating operation are performed on it). SortedMap will return entries based on the natural ordering of the keys, or a Comparator, if provided. LinkedHashMap will either return entries in insertion-order or access-order depending upon how it has been constructed. EnumMapreturns entries in natural order of keys.

answered Apr 17, 2018 by Daisy
• 8,120 points
0 votes

In Java 8 you can do it clean and fast using the new lambdas features:

 Map<String,String> map = new HashMap<>();
 map.put("SomeKey", "SomeValue");
 map.forEach( (k,v) -> [do something with key and value] );

 // such as
 map.forEach( (k,v) -> System.out.println("Key: " + k + ": Value: " + v));

The type of k and v will be inferred by the compiler and there is no need to use Map.Entryanymore.

answered Oct 24, 2018 by Sushmita
• 6,910 points

Related Questions In Java

0 votes
1 answer

How do I efficiently iterate over each entry in a Java Map?

To answer your questions, use the following ...READ MORE

answered Feb 8, 2022 in Java by Soham
• 9,700 points
416 views
0 votes
0 answers

For-each over an array in JavaScript

How can I loop through all the ...READ MORE

Feb 8, 2022 in Java by Rahul
• 9,670 points
213 views
0 votes
0 answers

For-each over an array in JavaScript

How can I loop through all the ...READ MORE

Apr 19, 2022 in Java by Rahul
• 3,380 points
313 views
0 votes
0 answers

For-each over an array in JavaScript

How can I use JavaScript to loop ...READ MORE

Sep 21, 2022 in Java by Nicholas
• 7,760 points
315 views
0 votes
2 answers

How can I sort values of a Map in Java using its key

Assuming TreeMap is not good for you ...READ MORE

answered Oct 10, 2018 in Java by Sushmita
• 6,910 points
917 views
0 votes
1 answer

How can I get Key from their respective Values

public class NewClass1 { ...READ MORE

answered May 16, 2018 in Java by Parth
• 4,630 points
484 views
0 votes
1 answer

Sort a HashMap in Java

Convert hashmap to an ArrayList with a ...READ MORE

answered Jul 13, 2018 in Java by Daisy
• 8,120 points
498 views
0 votes
2 answers

Ho do I Iterate through a HashMap which contains duplicate values

for (Map.Entry<String, String> item : params.entrySet()) { ...READ MORE

answered Jul 24, 2018 in Java by samarth295
• 2,220 points
4,814 views
0 votes
2 answers

How to create a 2-D array in java?

int[][] multi = new int[5][]; multi[0] = new ...READ MORE

answered Jul 16, 2018 in Java by Daisy
• 8,120 points
960 views
0 votes
3 answers

Check if a String is numeric in Java

Java 8 Lambda Expression is used: String someString ...READ MORE

answered Sep 3, 2018 in Java by Daisy
• 8,120 points
3,372 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP