Created
June 3, 2017 15:02
-
-
Save anil477/0980d96ff0a0dadf428735c931485b4f to your computer and use it in GitHub Desktop.
Just for comparison sake.
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
// Two ways just how to iterate a HashMap. Getting my hands dirty with Java :P | |
import java.util.*; | |
class Demo | |
{ | |
public static void main(String[] args) | |
{ | |
Map<String, String> dataSet = new HashMap<String, String>(); | |
dataSet.put("Chennai", "Banglore"); | |
dataSet.put("Bombay", "Delhi"); | |
dataSet.put("Goa", "Chennai"); | |
dataSet.put("Delhi", "Goa"); | |
for (Map.Entry<String,String> entry: dataSet.entrySet()) | |
System.out.println(" Key " + entry.getKey() + " Value " + entry.getValue() + " "); | |
System.out.println(""); | |
// Get a set of the entries | |
Set set = dataSet.entrySet(); | |
// Get an iterator | |
Iterator i = set.iterator(); | |
// Display elements | |
while(i.hasNext()) { | |
Map.Entry me = (Map.Entry)i.next(); | |
System.out.println(" Key " + me.getKey() + " Value " + me.getValue() + " "); | |
} | |
System.out.println(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment