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:

enter image description here

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

Popular posts from this blog

1111. appearing after print sequence - php -

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

Ruby on Rails, ActiveRecord, Postgres, UTF-8 and ASCII-8BIT encodings -