Skip to content

Instantly share code, notes, and snippets.

@anil477
Created June 3, 2017 15:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anil477/0980d96ff0a0dadf428735c931485b4f to your computer and use it in GitHub Desktop.
Save anil477/0980d96ff0a0dadf428735c931485b4f to your computer and use it in GitHub Desktop.
Just for comparison sake.
// 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