java - using varargs and Maps -
i want add object map gives error. maybe have use lists there getname() method used map. have check if has duplicate value , throw exception if found
public class handleapplications { map<string,name> names = new hashmap<>(); public void addnames(string... names) throws exception{ name c = new name(names); if(names.containsvalue(names)){ throw new applicationexception(); } names.put(names,c) //this line gives error } public name getname(string name) { return names.get(name); } }
i think need -
public void addnames(string... names) throws exception { (string s : names) { name c = new name(s); this.names.put(s, c); } }
consider changing variable names. variable names same, had use this.names refer map.
Comments
Post a Comment