object - Value of HashMap inside a Hashmap in Java -
i have hashmap of students stores id, name , last name. created :
map<interger, hashmap<string,string>> students = new hashmap<>(); where second hashmap stores name , lastname. goal student in swing application, succeed in searching id because it's key of first hashmap, i'd use of them this:
so question : if want search name or last name, how can value of first hashmap , put in new hashmap ?
you can iterate on hashmap :
private int searchbyname(string s) { for(map.entry<integer, hashmap<string, string>> entry : students.entryset()) { hashmap student = entry.getvalue(); // hashmap containing first , lastname if (student.containskey(s)) // if name match return entry.getkey(); // return student id } return 0; // student not found } for lastname use containsvalue(s) instead of containskey(s)

Comments
Post a Comment