/* package whatever; // don't place package name! */
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
class StateCityClassTest {
public static void main
(String[] args
) { StateCityMap map = new StateCityMap();
map.putStateCity(1253, "São Paulo", "Osasco");
map.putStateCity(1254, "São Paulo", "Santos");
map.putStateCity(1255, "Rio de Janeiro", "Cabo Frio");
System.
out.
println(map.
getStateCity(1253));
System.
out.
println(map.
showAllItens());
}
}
class StateCityMap {
private final Map
<Integer, HashMap
<String, String
>> codeStateList
;
public StateCityMap() {
codeStateList = new HashMap<>();
}
public void putStateCity
(int code,
String state,
String city
) { HashMap
<String, String
> stateCity
= new HashMap
<>(); stateCity.put(state, city);
codeStateList.put(code, stateCity);
}
public String getStateCity
(int key
) { return this.containsKey(key) ? codeStateList.get(key).toString() : null;
}
public void removeStateCity(int key) {
for (Iterator
<Map.
Entry<Integer, HashMap
<String, String
>>> iterator
= codeStateList.
entrySet().
iterator(); iterator.
hasNext();) { if (entry.getKey().equals(key)) {
iterator.remove();
}
}
}
public boolean containsKey(int key) {
return codeStateList.containsKey(key);
}
public String showAllItens
() { for (Map.
Entry<Integer, HashMap
<String, String
>> entry
: codeStateList.
entrySet()) { allItens += entry.getKey() + " : " + entry.getValue() + "\n";
}
return allItens;
}
}
LyogcGFja2FnZSB3aGF0ZXZlcjsgLy8gZG9uJ3QgcGxhY2UgcGFja2FnZSBuYW1lISAqLwoKaW1wb3J0IGphdmEudXRpbC5IYXNoTWFwOwppbXBvcnQgamF2YS51dGlsLkl0ZXJhdG9yOwppbXBvcnQgamF2YS51dGlsLk1hcDsKCgogY2xhc3MgU3RhdGVDaXR5Q2xhc3NUZXN0IHsKCiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB7CiAgICAgICAgU3RhdGVDaXR5TWFwIG1hcCA9IG5ldyBTdGF0ZUNpdHlNYXAoKTsKCiAgICAgICAgbWFwLnB1dFN0YXRlQ2l0eSgxMjUzLCAiU8OjbyBQYXVsbyIsIC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