The Artima Developer Community
Sponsored Link

Java Buzz Forum
3 Examples to Loop Map in Java - foreach, Iterator

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Javin Paul

Posts: 1090
Nickname: javinpaul
Registered: Jan, 2012

Javin Paul is Java Programmer working on Finance domain.
3 Examples to Loop Map in Java - foreach, Iterator Posted: May 8, 2014 6:57 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Javin Paul.
Original Post: 3 Examples to Loop Map in Java - foreach, Iterator
Feed Title: Java67
Feed URL: http://www.java67.com/feeds/posts/default?alt=rss
Feed Description: Java and technology tutorials, tips, questions for all programmers.
Latest Java Buzz Posts
Latest Java Buzz Posts by Javin Paul
Latest Posts From Java67

Advertisement
There are multiple ways to loop through Map in Java, you can either use foreach loop or Iterator to traverse Map in Java, but all ways 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 elements 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 8% hike, now our task is to update this Map, so each worker object reflect its new, increased salary. In order to do this task, we need to iterate through Map, get the Employee object, which is stored as value. Update Worker with new salary, and move on. Did you ask about saving it back to Map? no need; When you retrieve values from Map using get() method, they are not removed from Map, so one reference of same object is always there in Map, until you remove it. That's why, you just need to update 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. Internal code of  HashMap uses both of these method 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.
Read more »

Read: 3 Examples to Loop Map in Java - foreach, Iterator

Topic: Why you should not work extra hours Previous Topic   Next Topic Topic: How to Create Directory in Java Example

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use