java - check if a value already exists in a Hash Map -


have method in i'm trying iterate through values of hashmap , check if match specific string - if do, want method return false. had @ few topics , i've come although not seem work:

class person {   public int check(string newname, string newnumber) {          (person recordobject : map.values()) {              if (map.get(recordobject) != null                     && map.get(recordobject).tostring().equals(newnumber)) {                  return 0;             }         }          map.put(newname, newnumber);         return 1;     } } 

at moment program creates record person, if have been added map not go if of check method.

firstly, checking if value in map key in map...

 (person object : map.values()) {                  if (map.get(object) != null                         && map.get(object).getnumber().equals(newnumber)) {                      return false;                 }             } 

i assume mapping (string) name -> (person) person, by

map.put(name, record); 

is ideal mapping? if people have same name?

if want see if map has same key can not use method boolean personexistsinmap = map.containskey(string name)?

edit

another possible implementation have set<person>, , away hashmap since seems having name key rather pointless. override equals method in person.

set<person> personset = new hashset<person>();  if(personset.contains(person)) { //bla bla } 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -