which is the Best way to Iterate through a HashMap in java?

This is for Scala, Python, core Java, java script, Struts, Jsp, Servlet, Spring, EJB etc. Programming Language and Framework
mina1234
Posts: 72
Joined: Tue May 05, 2015 7:08 pm
Contact:

which is the Best way to Iterate through a HashMap in java?

Postby mina1234 » Thu Oct 01, 2015 5:33 pm

looking for Best way to Iterate through a HashMap in java. which is the Best way to Iterate through a HashMap in java?


user123
Posts: 91
Joined: Sun Aug 30, 2015 7:57 am
Contact:

Re: which is the Best way to Iterate through a HashMap in java?

Postby user123 » Thu Oct 01, 2015 6:01 pm

//iterate over keys
for (String key : hash.keySet()) {
System.out.println("Key = " + key);
}

//iterating over values only
for (String value : hash.values()) {
System.out.println("Value = " + value);
}

user123
Posts: 91
Joined: Sun Aug 30, 2015 7:57 am
Contact:

Re: which is the Best way to Iterate through a HashMap in java?

Postby user123 » Thu Oct 01, 2015 6:02 pm

while (entries.hasNext()) {
Map.Entry entry = (Map.Entry) entries.next();
String key = (Integer)entry.getKey();
String value = (Integer)entry.getValue();
System.out.println("Map Key = " + key + ", Map Value = " + value);
}



Return to “Core Java, Scala, Python and Advanced Programming or development”

Who is online

Users browsing this forum: No registered users and 3 guests