Created
August 26, 2014 02:49
-
-
Save zeusbaba/2604c2e96f1fa368f4d2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*** | |
* Copyleft - incognito | |
* | |
* @author: incognito_at_wareninja | |
* @see https://github.com/WareNinja | |
* disclaimer: I code for fun, dunno what I'm coding about :) | |
*/ | |
package utils; | |
import java.util.HashMap; | |
import java.util.Iterator; | |
import java.util.LinkedHashMap; | |
import java.util.Map; | |
/** | |
* How To Iterate Through Java Map or Hashmap | |
* source adapted from; | |
* http://tutorialswithexamples.com/how-to-iterate-through-map-or-hashmap-in-java/ | |
* http://www.sergiy.ca/how-to-iterate-over-a-map-in-java/ | |
*/ | |
public class MapIteratorExample { | |
final String TAG = MapIteratorExample.class.getSimpleName(); | |
public static void main(String[] args) { | |
Map<String, String> mapData = new LinkedHashMap<String, String>(); | |
mapData.put("key-1", "val-1"); | |
mapData.put("key-2", "val-2"); | |
mapData.put("key-3", "val-3"); | |
mapData.put("key-4", "val-4"); | |
mapData.put("key-5", "val-5"); | |
/** | |
* Method #1: Using EntrySet and advanced for loop. | |
* This method is not suitable for modifying the Map | |
* while iterating through it. | |
*/ | |
System.out.println("Using EntrySet"); | |
for(Map.Entry<String, String> mapEntry : mapData.entrySet()){ | |
System.out.println(mapEntry.getKey() +" :: "+ mapEntry.getValue()); | |
//if you uncomment below code, it will throw java.util.ConcurrentModificationException | |
//mapData.remove("key-1"); | |
} | |
/** | |
* Method #2: Using KeySet and advanced for loop. | |
* This method is not suitable for modifying the Map | |
* while iterating through it. | |
*/ | |
System.out.println("Using KeySet"); | |
for(String key: mapData.keySet()){ | |
System.out.println(key +" :: "+ mapData.get(key)); | |
//if you uncomment below code, it will throw java.util.ConcurrentModificationException | |
//mapData.remove("key-1"); | |
} | |
/** | |
* Method #3: Using Iterator and while loop. | |
* In this method you can remove the entries | |
* while iterating through it. | |
*/ | |
System.out.println("Using Iterator"); | |
Iterator<Map.Entry<String, String>> iterator = mapData.entrySet().iterator() ; | |
while(iterator.hasNext()){ | |
Map.Entry<String, String> mapEntry = iterator.next(); | |
System.out.println(mapEntry.getKey() +" :: "+ mapEntry.getValue()); | |
//You can remove elements while iterating. | |
iterator.remove(); | |
} | |
/** | |
* Method #4: Iterating over keys and searching for values | |
* (inefficient-slow). | |
* | |
*/ | |
System.out.println("Using KeySet (alt.2)"); | |
for (String key : mapData.keySet()) { | |
String value = mapData.get(key); | |
System.out.println("Key = " + key + ", Value = " + value); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment