In this tutorial, we will discuss Java HashMap/Hashtable, LinkedHashMap, and TreeMap. A HashMap however, store items in "key/value" pairs, and you can access them by an index of another type (e. HashMap member function entrySet() returns a set of Entry in the HashMap and its backed by HashMap i. The steps we followed in the below example are as follows: 1) Create a HashMap and populate it with key-value pairs. On the other hand, a HashSet is an implementation of Set interface. Iterate through HashMap using Iterator : This method uses java. The example in this section shows various ways of iterating over a LinkedHashMap: Iterate over a LinkedHashMap using Java 8 forEach and lambda expression. Hashmap uses the array of Nodes(named as…. Since all maps in Java implement Map interface, following techniques will work for any map implementation (HashMap, TreeMap, LinkedHashMap, Hashtable, etc. Entry object and by looping over them, you can easily get access to key and value object. Using Iterator. How to iterate through HashMap? Iterating through map or hashmap in java. java hashmap example (6) Depends. 86 * Map m = Collections. Java source code. I love Java collection and have multiple tutorials on How to iterate through Map and List, LinkedList, JSONArray and lot more. Simply put, we can extract the contents of a Map using keySet(), valueSet() or entrySet(). A HashMap stores a collection of objects, each referenced by a key. The Set is backed by the HashMap, so changes to the HashMap are reflected in the Set, and vice-versa. javaProgram; // Iterate through HashMap entries(Key-Value pairs). looping through hashmap. next (); System. In this example we are gonna see how to iterate a HashMap using Iterator and display key and value pairs. LinkedHashMap will iterate in the order in which the entries were put into the map "Hashtable" is the generic name for hash-based maps. this will iterate through all values of Map and display key and value together. Entry object and use for loop to iterate over them. Iterate through HashMap using Iterator : This method uses java. 浙公网安备 33030202000166号. It serves all basic needs of original java. In the previous tutorial we have seen different-2 ways to iterate a HashMap. put("a","a1"); map. Null keys/values hashmap allows one null, hashtable does not allow any 3. Declaration. In the scope of this article, I'll explain: HashMap internal implementation; methods and functions and its performance (O(n) time complexity) collisions in HashMap; interview questions and best practices. It stores value in the form of key/values pair. It is used to store key & value pairs. This implementation provides all of the optional map operations, and permits null values and the null key. HashMap/Hashtable. It is used to iterate through the hashmap and also reduces the number of lines of code. HashSet Vs HashMap in Java As the names imply, a HashMap is an associative Map and HashSet is just a Set. Iterating over keys or values using keySet() and values() methods Map. NET to Java, from iOS to Android, from Python to PHP and ROR, and many others! How To Iterate the Hashtable in C# - BurnIgnorance. The HashMap code very smartly appends the new value right at the beginning. If you keep looking up the same key over and over, HashMap goes through all the work over and over, except for computing the hashCode. First of all, we need to import required packages: [crayon-5db48eb53a9a6006077660/] Then, naturally, set our HashMap and put some data in there: [crayon-5db48eb53a9b0729665109/] 1. Each key is mapped to a single value in the map. The following complete example shows how to iterate over all of the elements in a Java Map (or HashMap) using both a) the Java 8 style and b) the type of code you had to use prior to Java 8:. With Java 8 forEach statement provides a new way to loop a HashMap in Java. The method keySet() returns all key entries as a set object. Best way to Iterator over HashMap in Java is by using Map. Java Examples: Collections - Iterate Through A HashMap. HashMap is one of the most useful collections in Java. Use this approach if you are interested in retrieving both keys and values stored in the HashMap. The Map is an interface in Java like the List and Set are interfaces of Collection. I've used HashMap and TreeMap, but I have yet had the need to use a LinkedHashMap. Iterate or Loop HashMap in Java Using entrySet() and java iterator In this example we have used combination of Iterator and entrySet() to show all keys and values of a Java Map. Check this thread at java forum: Hashtable Vs HashMap. Give HashMap plenty of spare space to work in the constructor. We can see that forEachRemaining and forEach are new APIs introduced in Java 8, can be used to iterate over collections like Set, List, etc. The steps we followed in the below example are as follows: 1) Create a HashMap and populate it with key-value pairs. Iterating through each key, we can get corresponding value object. Iterate through HashMap using Iterator : This method uses java. HashMap examples. HashSet Constructor snippets. Then you can get rid of the casts in lines 18 and 22. You can iterate over HashMap entries through keys set but they are not guaranteed to be in the order of their addition to the HashMap. HashMap contains a variable to count the number of modifications and iterator use it when you call its next() function to get the next entry. 86 * Map m = Collections. Program for Sorting HashMap by Keys. synchronizedMap(new HashMap());. next() to move through the HashMap, get values, and add them to the tree by iterating through the tree until you find a suitable place to put the value. The following example shows different ways of iterating over a HashMap - Iterating over a HashMap using Java 8 forEach and lambda expression. Learn How to loop a HashMap iterator in Java Program. Let's first look at what it means that HashMap is a map. Java hashmap a complete tutorial for beginners with examples, methods and functions, explanation of each class from java key value pair to put method. Description: Below example shows how to read add elements from HashMap. Full source code can be downloaded from here To deep dive into the fundamentals of java. What is the fastest and the best way to iterate over the elements of a HashMap. It doesn’t require an Iterator and is thus more compact and probably more efficient. As other methods of HashMap class, Generics should also be considered in using the remove() method. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. Collections are enhancement to the arrays. entrySet() for iterating key/value pairs. HashSet Vs HashMap in Java As the names imply, a HashMap is an associative Map and HashSet is just a Set. The method call returns a set view of the keys contained in this map. Print both key and values of the LinkedHashMap using one while loop. HashMap is one of the most used collections in java. md Find file Copy path tuxiantian Update iterate-through-a-hashmap. In that case we don’t need iterate and it will take less coding. Concurrent structures and collections in Java 5. What is the easiest/best/most correct way to iterate through the characters of a string in Java? Ask Question; What is the easiest/best/most correct way to iterate through the characters of a string in Java? iterate through SparseArray; How do I efficiently iterate over each entry in a Java Map? How to update a value, given a key in a hashmap?. Original question: How do I iterate through a hashmap using a loop in Java? Answer: This is a simple google search away or you can easily browse StackOverFlow for the answer. Keys are unique. stackoverflow-java-top-qa / contents / iterate-through-a-hashmap. When you need a Map and you don't care about the order when you iterate through it, then HashMap is the way to go; the other maps add a little more overhead. Java HashMap is the basic implementation of Map interface. What am I doing wrong? There should only be an ArrayList per. 工信部备案号:浙ICP备09062716号-2 ©2005-2017 温州第七城市信息科技有限公司 Inc. We will try some of them. were in we can remove hash map key/value pair through. As of Java 8, we can use the forEach method as well as the iterator class to loop over a map. Each iteration will give you inner HashMap on object outerHash. Depending on your Java version you can choose one of them. Each key is mapped to a single value in the map. A HashMap however, store items in "key/value" pairs, and you can access them by an index of another type (e. The foreach loop is generally used for iteration through array elements in different programming languages. Let’s see an example below which we iterate over a HashMap using an iterator. It is only available since Java 5 so you can't use it if you are restrained to Java 1. In other words, you can set the specific type of both the keys and values in a generic Map instance. Arguably, each count will be "in and-out", and one might argue that the improvement in throughput over a regular synchronized hash map. Iterate through the values of Java HashMap. Lets go over the most common methods and review their advantages and disadvantages. It is quite powerful as it can be used as a higher order functions through lambda functions and above examples could help you to get better idea on how to implement it. HashMap-single key multiple values using LIST import java. There are several ways of iterating over a Map in Java. This iteration is less efficient. But when it comes to accessing the values stored in a HashMap, many people don't know how to iterate through a HashMap to get the values. 3) Obtain the iterator for entry set. There are several ways of iterating over HashMap. Print both key and values of the LinkedHashMap using one while loop. There is also a similar function that returns a Set that contains all the keys in your hasmap. Iterating a Map is not like a List Iteration. A bad program would probably iterate through the bucket list, adding the key at the end. A HashMap is a simple yet powerful way to store and get data. 75=12; 这个很重要,对效率很一定影响!当存入HashMap的对象超过这个容量时,HashMap 就会重新构造存取表。. The keys of a dictionary in Python are generated by a hashing function. You can then simply iterate through this set using a regular set iterator. Make sure your hash function is quick, possibly caching the result. THE unique Spring Security education if you're working with Java today. Java Examples: Collections - Iterate Through A HashMap. In this quick article, we'll have a look at the different ways of iterating through the entries of a Map in Java. In the next example, we iterate over a key set with an iterator while using HashMap's keySet() method, which returns a Set view of the keys contained in this map. Java hashmap a complete tutorial for beginners with examples, methods and functions, explanation of each class from java key value pair to put method. I want to iterate through each key of a hashmap and compare it to each key below it (so I don't compare keys twice). You can instead use the following code to iterate through each entry. 87 * 88 *. Java HashMap class contains only unique keys. Java HashMap tutorial: HashMap in java HashMap internal working hash and indexfor method in HashMap hashcode and equals in java sort HashMap by keys and values Difference between HashMap and HashSet Difference between HashMap and Hashtable How to iterate over HashMap Sorting by. For loop; While loop + Iterator; Example: In the below example we are iterating the HashMap using both the methods (for loop and while loop). Check if a particular key exists in Java HashMap example. hashmap as if it were the same as java. There is also a similar function that returns a Set that contains all the keys in your hasmap. any items remover from the Entry Set will be removed from HashMap too. There are multiple ways to iterate through a Java Map. Java 5 introduced generic collections. Iterate over a LinkedHashMap's entrySet using Java 8 forEach and lambda expression. In this example we are gonna see how to iterate a HashMap using Iterator and display key and value pairs. Using for each to iterate through a HashMap. HashMap is not an ordered collection. 0 version. Iterate or Loop HashMap in Java Using entrySet() and java iterator In this example we have used combination of Iterator and entrySet() to show all keys and values of a Java Map. Ok, i'm not sure of the internal structure of a Hashtable but isn't it really inefficient to be iterating over the keys, then for each key have to do a lookup via get()? I would have thought the iterator would just give you a pointer to both key and value like HashMap in later versions of java. There are a few different types of maps. How to iterate Map in Java. looping through hashmap. keySet() for iterating keys, Map. Java HashMap is not a thread-safe implementation of key-value storage, it doesn't guarantee an order of keys as well. Hashmap uses the array of Nodes(named as…. Any Map’s implementation has to provide the following three Collection view methods: keySet(): returns a Set view of the keys contained in the. Arms Of Gold Crest England Engraved Cufflinks Horlen Coat Coat Arms Box Surname Family 5056166533550. But how many developers know how a HashMap works internally? A few days ago, I’ve read a huge part of the source code of java. Basic java example program to iterate keys of hashtable Iterate through keys of hashtable java - InstanceOfJava This is the java programming blog on "OOPS Concepts" , servlets jsp freshers and 1, 2,3 years expirieance java interview questions on java with explanation for interview examination. There are a few different types of maps. Also I post on Google+,so you can add me in your circle to get updates. C++11 provides a range based for loop, we can also use that to iterate over the map. Processing Forum Recent Topics. Let's first look at what it means that HashMap is a map. It is used to iterate through the hashmap and also reduces the number of lines of code. [Java]Simplest way of iterating through HashMaps? When iterating through arrays or ArrayList, you usually just simply use a for or maybe an enhanced for loop but with HashMaps, I am frankly not sure what is the easiest way. Unlike Lists, and ArrayLists should come to mind, Maps don't have an iterator() method like that provided by the Set or List classes. Removing Entries from Map in Java: One reason for iterating over Map is removing selected key value pairs from Map. HashMap is generally preferred, unless you have to deal with threads and synchronization (not a topic for this course). Hashmap uses the array of Nodes(named as…. You can instead use the following code to iterate through each entry. Unlike other Collections, we cannot iterate through HashMap directly we need to get the keySet or entrySet to iterate. As you can see, there are many ways to loop over a Map, using for, foreach, tuples, and key/value approaches. values() method returns a collection-view of the values contained in this map. In one of the previous chapters, we have described arrays. In the next example, we iterate over a key set with an iterator while using HashMap's keySet() method, which returns a Set view of the keys contained in this map. The Java provides arrays as well as other collections and there should be some mechanism for going through array elements easily; like the way foreach provides. In summary, I hope these examples of iterating a Scala Map have been helpful. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. TreeMap is an example of a SortedMap and is implemented by is implemented by Red-Black tree, which means that the order of the keys is sorted. Each iteration will give you inner HashMap on object outerHash. In this tutorial I will explain you how you can iterate through HashMap using the entrySet() method of HashMap class. The object is the key. Please don't forget it mention it in the comment section. HashMap Single Key and Multiple Values. A Map, as you might know, is a collection of key-value pairs. How do IStore simple pairs of data for quick lookup?. Keys are unique. Iterating over the HashMap’s entrySet using Java 8 forEach and lambda expression. A HashMap however, store items in "key/value" pairs, and you can access them by an index of another type (e. There are 5 ways you can iterate through List. 6 ways to Iterate elements in a HashMap in java with example VK December 22, 2014 collection , core java Like Hashtable, HashMap and TreeMap does not provide the methods elements() and keys() to return enumeration of the values and keys respectively to process all the elements. Iterate through HashMap using Iterator : This method uses java. Hashmap or treemap in java and we all familiar of either all of those or some of those. Print both key and values of the LinkedHashMap using one while loop. It stores value in the form of key/values pair. HashMap is one of the most useful collections in Java. Hashmap or treemap in java and we all familiar of either all of those or some of those. From Java 8 you can use a Java Stream to iterate the keys of a Java Map. That is,when iterating through a collection-view of a LinkedHashMap, the elements will be returned in the order in which they were inserted. Iterate over a LinkedHashMap's entrySet using iterator(). Entries can be retrieved from the HashMap with a variety of methods. As of Java 8, we can use the forEach method as well as the iterator class to loop over a map. With for-each loop, use Map. 11 abgn aggregation framework ant Apache apache commons aws backup bgn build cassandra CentOS code Collection command line cql Data Science distinct EC2 ethernet card fedora fedora core 5 find ftp How To Howto iptables Java java6 Javascript jvm debugging Linux Linux How To Linux Tip Machine Learning Mac OS X macports map reduce math. Depending on your Java version you can choose one of them. Check if a particular key exists in Java HashMap example. Iterate through a HashMap - Java Lets go over the most common methods and review their advantages and disadvantages. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. This is best done at creation time, to prevent accidental 85 * unsynchronized access to the map:. If you keep looking up the same key over and over, HashMap goes through all the work over and over, except for computing the hashCode. Each iteration will give you inner HashMap on object outerHash. The Eclipse Foundation - home to a global community, the Eclipse IDE, Jakarta EE and over 350 open source projects, including runtimes, tools and frameworks. for (String key: keys) How HashMap Works in Java. The tutorial has shown us how to iterate over a List or Set in Java 8. Also if one inserts the key again into the LinkedHashMap the original orders are retained. Include a class UserMainCode with a static method sizeOfResultandHashMap which accepts hashmap as input. Through simple and precise examples. HashMap member function entrySet() returns a set of Entry in the HashMap and its backed by HashMap i. public class HashMap extends AbstractMap implements Map, Cloneable, Serializable. This is similar to an Array, only instead of accessing elements with a numeric index, a String is used. The method keySet() returns all key entries as a set object. void forEach(Consumer action) In this Java 8 , ForEach Example article lets see how to iterate through a List and Map with the new forEach Loop in Java 8. Entry object and use for loop to iterate over them. How to iterate through a map in Java? Posted on September 27, 2012 October 2, 2013 by Devesh Sharma in Java Here is the code to iterate through a map in Java. How to traverse or loop Map, HashMap or TreeMap in Java In next section of this Java tutorial, we will see four different ways of looping or iterating over Map in Java and will display each key and value from HashMap. If you using Java 8 this is the easiest way to loop the Map. C++ equivalent of Java HashMap: sample code for porting I'm an experienced Java programmer with some knowledge of C and very, very little knowledge of C++. To Iterate or loop a map we have 3 ways using with the help of Map. To use HashMap in multi-threading you need to synchronize the HashMap as Collections. In Java, iteration over Map can be done in various ways. Duplicate keys are not allowed. I want to iterate through the data, how to iterate through hashmap and store entities into? How to iterate through list of hashmap data? Thymeleaf, value pairs, the hashmap object contains two key, iterating through each key, jsp, user forum. Iterating over the HashMap's entrySet using iterator(). A new key could be added anywhere in the bucket. Method #2: Iterating over keys or values using a For-Each loop. Iterating over keys or values using keySet() and values() methods Map. It crops up extremely frequently in programming in all sorts of cases where we want to deal with cases of "for X, what is the Y"?. 0 version, we can take advantage of Enumeration interface to iterate through Hashtable. Hello! geeks, this blog welcomes the entire Java family for an exploration of a very powerful tool for Organizing and Manipulating our data known as the absolute Java Collection Framework. Learn How to loop a HashMap iterator in Java Program. You first obtain the key Set from the Map and from that you can get a Stream. Learn Java - How To Iterate Through a HashMap October 14, 2016 The use of HashMap is very common when working with values where it doesn't matter the position of the item but the value of the key. The HashMap gives you an unsorted, unordered Map. Remove all values from Java HashMap example; Get Size of Java HashMap Example; Check if a particular value exists in Java HashMap Check if a particular key exists in Java HashMap e How to get all keys in Java HashMap? Get Set view of Keys from Java HashMap example; Iterate through the values of Java HashMap example Simple Java HashMap. In the context of the Java API, Hashtable is an obsolete class from the days of Java 1. HashMap is an object that stores both key=value as a pair. Our HashMap looks like this. HashMap Class in Java. entrySet() returns a set of key-value mappings contained in the map. I love Java collection and have multiple tutorials on How to iterate through Map and List, LinkedList, JSONArray and lot more. Arguably, each count will be "in and-out", and one might argue that the improvement in throughput over a regular synchronized hash map. HashMap with O(1) complexity in read operations. It means we can insert a key ‘K’ only once in a map. On the other hand, a HashSet is an implementation of Set interface. The steps we followed in the below example are as follows: 1) Create a HashMap and populate it with key-value pairs. If my last similar post, I tried to compare different "for loop" flavors available in java. It means we can insert a key 'K' only once in a map. There are many ways to iterate through HashMap’s in java. HashSetare use to store only value or element. Check out the following example,. Original question: How do I iterate through a hashmap using a loop in Java? Answer: This is a simple google search away or you can easily browse StackOverFlow for the answer. Hashmap or treemap in java and we all familiar of either all of those or some of those. How to get Keys and Values from HashMap in Java? Example program of iterating through HashMap. Iterate through HashMap using Iterator : This method uses java. com - How to Iterate an HashMap. Hi there, I'm having a HUGE issue with iterating through my HashMap from another class. Java provides a similar syntax for iterating over the items in Lists and Sets. Example of getting keys and values from HashMap in Java using the entrySet() method of HashMap. But to my surprise, one of my friends was asked in his interview (he has more than 6 years of experience in Java programming) to write code for getting values from hashmap or TreeMap in Java. Note that for some collection, such as List, we still can use the index to traverse normally. I want to iterate through the data, how to iterate through hashmap and store entities into? How to iterate through list of hashmap data? Thymeleaf, value pairs, the hashmap object contains two key, iterating through each key, jsp, user forum. You can iterate over HashMap entries through keys set but they are not guaranteed to be in the order of their addition to the HashMap. We have taken the set of keys from HashMap once and then iterating over it. Method #2: Iterating over keys or values using For-Each loop. txt) or read online for free. There are four common types of modification you might want to do to the keys or values in a HashMap. As a Java developer, everyone should know how to Iterate through HashMap, as it will be part of his routine programming. Some of the important points about HashMap in Java are; Java HashMap allows null key and null values. Following, the three common methods for iterating through a Collection are presented, first using a while loop, then a for loop, and finally a for-each loop. How to iterate a Java 8 Map: A complete example. entrySet() returns a set of key-value mappings contained in the map. You can instead use the following code to iterate through each entry. HashMap contains a variable to count the number of modifications and iterator use it when you call its next() function to get the next entry. To use the Dictionary, install npm install --save @types/lodash Below code illustrates the following scenarios initalize the Dictionary, iterate over dictionary, check if particular key is already present, add an element in dictionary, get the length of the dictionary, delete an element from dictionary, get key array from dictionary, get value array from dictionary. Though a value 'V' can be mapped to. Iterating over the HashMap's entrySet using Java 8 forEach and lambda expression. We will start with a simple use case and will explore how we can do this using Java 8 Stream API. The keySet() method returns the Set of all the Keys in the HashMap. In the next example, we iterate over a key set with an iterator while using HashMap's keySet() method, which returns a Set view of the keys contained in this map. In Java 8 - How to sort a Map? On Crunchify we have written almost ~400 java tutorials and this one is an addition to Java8 category. Finally, I will like to hear your suggestion and thoughts. It is available in java. A HashMap stores a collection of objects, each referenced by a key. There is also a similar function that returns a Set that contains all the keys in your hasmap. In this article you will learn how to iterate through HashMap in java. HashMap in Java is a hashtable implementation of the Map interface which provides all the optional Map operations. Java collections. In Java 8 a new method has been introduced for Iterating over collections in Java. There are 5 ways you can iterate through List. 1 before the collections framework existed. Iterating through a map is a common task that you will find in your day to day programming job. util package which is used to iterate through a collection. package com. HashMap is a hash table based implementation of Map interface. As of Java 8, we can use the forEach method as well as the iterator class to loop over a map. HashMap is one of the most useful collections in Java. Example of getting keys and values from HashMap in Java using the entrySet() method of HashMap. C / C++ Forums on Bytes. The keySet() method returns the Set of all the Keys in the HashMap. Lets go over the most common methods and review their advantages and disadvantages. Duplicate keys are not allowed. entrySet Iterators are also used to loop through the hashmaps. It stores value in the form of key/values pair. Use entrySet() method of the Java HashMap to get the Set view of stored mappings in form of Map. So If you need only keys or values from the map, you can iterate over keySet or values using for-each loops. These studies usually help in setting up best practices for your next project. How To Iterate Through Map or Hashmap in Java - TutorialsDesk. use data-sly-list. Java 8 LongFunction Interface is an absolute useful addition as part of ‘Functional Interfaces’ and can serve variety of purposes. Method #2: Iterating over keys or values using a For-Each loop. Check if a particular key exists in Java HashMap example. HashMap is a hash table based implementation of Map interface. If you using Java 8 this is the easiest way to loop the Map. Iterating key-value pairs in a HashMap using entrySet() method In this example we will create another Hashtable to store objects in the form of key-value pair, where key is a String object and value is an Integer object. I was just putting this in as a reference as to how to iterate through all keys and values in an object. We can iterate LinkedHashMap using Iterator or for loop or while loop etc. HashMap, Hashtable, LinkedHashMap or even relatively new ConcurrentHashMap. How to iterate through HashMap? Iterating through map or hashmap in java. md 77a1c0f Jun 8, 2017. println Iterate Through A HashMap Iterator Example. A HashMap is an implementation of Map interface. Not a list. Hashmap or treemap in java and we all familiar of either all of those or some of those. Hashtable is what the Sun calls, the legacy collection implementation. keySet() method returns a view of the keys contained in the map. 2) HashMap is not part of collection inteface. In this example we are gonna see how to iterate a HashMap using Iterator and display key and value pairs. // iterate through the key set and display key and values. Java HashMap tutorial: HashMap in java; How HashMap works in java; hash and indexfor method in HashMap; hashcode and equals method in java; How to sort HashMap by keys and values; Difference between HashMap and Hash Set. Extracted from the reference How to Iterate Over a Map in Java: There are several ways of iterating over a Map in Java. Using forEach() method: forEach() is a method of hashmap which is introduced in java 8. In this post, we will discuss various methods to iterate Map using keySet() in Java. How to traverse or loop Map, HashMap or TreeMap in Java In next section of this Java tutorial, we will see four different ways of looping or iterating over Map in Java and will display each key and value from HashMap. If the map is modified while an iteration over the set is in progress (except through the iterator's own remove operation), the results of the iteration are undefined. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. I have this method for getting the phone numbers of a certain person now but the problem is it returns all the values in one print. Java HashMap class contains only unique keys. Re: How to iterate HashMap? Hello every body, I think i am facing a similar problem too I have a form with th:object="formBean" This object contains an attribute called "carMap = HashMap" I try to create input with the value of this map, but it does not work. Hashtable is a legacy data structure from earlier versions of Java. A Map, as you might know, is a collection of key-value pairs. In that case we don't need iterate and it will take less coding. Then you can get rid of the casts in lines 18 and 22. A common method for iterating over a Java collection is to use an enhanced for loop, which has the form: for (element: collection) statement.