tag:blogger.com,1999:blog-694855878384792308.post5672348330674922452..comments2024-03-21T06:26:49.387-07:00Comments on Java67: 3 Examples to Loop Map in Java - Foreach vs Iteratorjavin paulhttp://www.blogger.com/profile/15028902221295732276noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-694855878384792308.post-82180548684949731312016-01-20T22:54:26.609-08:002016-01-20T22:54:26.609-08:00In this code worker object we are not used as key ...In this code worker object we are not used as key then I didn't understand overriding of hashcode and equals in Worker object.Please correct me if I am wrongAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-80971224884580330652014-12-11T22:25:02.413-08:002014-12-11T22:25:02.413-08:00While looping over Map you must remember that Map ...While looping over Map you must remember that Map doesn't guarnatee any order, except TreeMap which guaranteeds sorting order and LinkedHashMap which keeps entries in the order they were inserted into Map. Other than that no Map guarantee any order, not even HashMap. For simple read only iteration the techniques you mentioned is sufficient, especially the one which gets entrySet() and then loop over Map using foreach loop. But, if you have to remove entries during iteration, there is no choice other than using Iterator. Also note, since HashMap is not synchronized, you may be iterating over stale mappings if there is another thread which is modifying Map in background. ConcurrentModificationException is also on the cards. Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-694855878384792308.post-15717059155593764572014-05-15T15:26:20.789-07:002014-05-15T15:26:20.789-07:00Thanks for sharing, very useful blog Thanks for sharing, very useful blog Anonymoushttps://www.blogger.com/profile/08495720753336825943noreply@blogger.com