Iterating map in java => http://paydehoukab.nnmcloud.ru/d?s=YToyOntzOjc6InJlZmVyZXIiO3M6MjE6Imh0dHA6Ly9iaXRiaW4uaXQyX2RsLyI7czozOiJrZXkiO3M6MjE6Ikl0ZXJhdGluZyBtYXAgaW4gamF2YSI7fQ== In this post, we will discuss various methods to iterate Map using keySet in Java. We will still use entrySet for performance reason, but we will use Iterator's remove method for deleting entries from Map. There are generally five ways of iterating over a in Java. Consider that a test result of x±e implies that there were result within the interval from x-e to x+e, so the fastest result 1184. In this post, we will discuss various methods to iterate Map using keySet in Java. The official way to do this is to call map. If you need only keys or values from the map, you can iterate over keySet or values instead of entrySet. If you are stuck with older version of Java less than 5 or planning to remove entries during iteration you have to use method 3. Entry, but here we use. So we can iterate a map using keySet and for each key calling map. However, if you love the Lambda style of Java 8, you can use the foreach, and for the remaining purpose, you can choose 2 remaining ways. Should be used if you need both map keys and values in the loop. It is very basic concept to crack java interview for freshers. 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 reflects its new, increased salary. Then, as usual, we loop through the iterator with iterator. Java 8 - This makes it also the fastest way to loop over HashMap in Java. So potentially this can remove the need for iterating in the first place - you might be able to find the specific entry you are after using the higherEntry, lowerEntry, ceilingEntry, or floorEntry methods. What is the best way to Iterate over HashMap in Java. Well, when it comes to choosing betweenit's you need, which plays an important role. For example, if you just want to iterate over each entry of HashMapwithout modifying Mapthen iterating over entry set using Java 1. The reason, it just two lines of code using a foreach loop and Generics, and by getting the set of entries, we get key and value together, without further searching in HashMap. This makes it also the fastest way to loop over HashMap in Java. On the other hand, if you want to remove entries, while iterating over HashMap, may be only selective entries, than foreach loop will not help. Though foreach loop internally uses Iterator for traversing elements, It doesn't expose handle to that Iterator, which means you only have remove method of Map to remove entries. That's not the ideal way, especially if you had to remove lot of entries and that to during Iteration. Your Iterator may also throw ConcurrentModificationExceptiondepending upon it's. Iterator of ConcurrentHashMap are weekly consistent with actual Map and doesn't throw ConcurrentModificationException. This leaves us with traditional while loop and Iterator combo, for looping HashMap using Map. Iterating map in java way to Iterate over HashMap in Java Here is the code example of Iterating over any Map class in Java e. Though I have used HashMap for iteration purpose, you can apply same technique to other Map implementations. Since we are only using methods from java. Map interfacesolution is extensible to all kinds of Map in Java. We will use Java 1. Entry object, which we get by calling Map. Remember to use Generics, to avoid type casting. Use of Generics and foreach loop result in very concise and elegant code, as shown below. In next section, we will see code using Iterator, which can help you for removal of entries from Map. One reason for iterating over Map is removing selected key value pairs from Map. This is a general Map requirement and holds true for any kind of Map e. HashMapHashtableLinkedHashMap or even relatively new ConcurrentHashMap. When we use foreach loop, it internally translated into Iterator code, but without explicit handle to Iterator, we just can not remove entries during Iteration. If you do, your Iterator may throw ConcurrentModificationException. To avoid this, we need to use. We will still use entrySet for performance reason, but we will use Iterator's remove method for deleting entries from Map. We have seen couple of ways to iterate over each entry, but as I said, best way is to use foreach loop and entry set, if you just need to traverse, without modifying actual Map. It's very elegant for filtering, where you need to create another Mapfrom actual Mapbased on some filtering criterion. Entry object holds both key and value, it provides fastest access to them, instead of calling Map. Further Learning Anonymous In my opinion best way to iterate a Map is by using it's keySet instead of entrySet because you often need keys to perform filtering or any kind of processing logic, and only require value on need basis. By using entrySetyour Set will require more memory as they now hold both key iterating map in java value.