Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get object from HashMap respectively? [duplicate]

Tags:

java

hashmap

Possible Duplicate:
How do I iterate over each Entry in a Map?

I make the personal profiles database,and I use HashMap to collect profile.

private HashMap<String, Profile> database;

but I want to write profile data to text files

PrintWriter fileOut = new PrintWriter(new FileWriter(fileName));
fileOut.println(database.size());
for(int i = 0; i < database.size();i++){
 Profile eachProfile = database.get(key);
}

But I don't know how to get list of key to looping How can I get data from HashMap respectively with another ways?

like image 296
DrNutsu Avatar asked Sep 26 '12 09:09

DrNutsu


1 Answers

You could use Map.entrySet() and extended for:

for (Map.Entry<String, Profile> e: database.entrySet())
{
    String s  = e.getKey();
    Profile p = e.getValue();
}
like image 184
hmjd Avatar answered Oct 03 '22 23:10

hmjd