Sunday 25 September 2016

java - How do I get object from HashMap respectively?











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



private HashMap 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?


Answer



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




for (Map.Entry e: database.entrySet())
{
String s = e.getKey();
Profile p = e.getValue();
}

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...