fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.HashMap;
  4. import java.util.Iterator;
  5. import java.util.Map;
  6.  
  7.  
  8. class StateCityClassTest {
  9.  
  10. public static void main(String[] args) {
  11. StateCityMap map = new StateCityMap();
  12.  
  13. map.putStateCity(1253, "São Paulo", "Osasco");
  14. map.putStateCity(1254, "São Paulo", "Santos");
  15. map.putStateCity(1255, "Rio de Janeiro", "Cabo Frio");
  16.  
  17. System.out.println(map.getStateCity(1253));
  18.  
  19. System.out.println(map.showAllItens());
  20.  
  21. }
  22.  
  23. }
  24.  
  25. class StateCityMap {
  26.  
  27. private final Map<Integer, HashMap<String, String>> codeStateList;
  28.  
  29. public StateCityMap() {
  30. codeStateList = new HashMap<>();
  31. }
  32.  
  33. public void putStateCity(int code, String state, String city) {
  34. HashMap<String, String> stateCity = new HashMap<>();
  35. stateCity.put(state, city);
  36. codeStateList.put(code, stateCity);
  37. }
  38.  
  39. public String getStateCity(int key) {
  40. return this.containsKey(key) ? codeStateList.get(key).toString() : null;
  41. }
  42.  
  43. public void removeStateCity(int key) {
  44. for (Iterator<Map.Entry<Integer, HashMap<String, String>>> iterator = codeStateList.entrySet().iterator(); iterator.hasNext();) {
  45. Map.Entry<Integer, HashMap<String, String>> entry = iterator.next();
  46. if (entry.getKey().equals(key)) {
  47. iterator.remove();
  48. }
  49. }
  50. }
  51.  
  52. public boolean containsKey(int key) {
  53. return codeStateList.containsKey(key);
  54. }
  55.  
  56.  
  57. public String showAllItens() {
  58. String allItens = "";
  59. for (Map.Entry<Integer, HashMap<String, String>> entry : codeStateList.entrySet()) {
  60. allItens += entry.getKey() + " : " + entry.getValue() + "\n";
  61. }
  62. return allItens;
  63. }
  64. }
  65.  
Success #stdin #stdout 0.06s 320576KB
stdin
Standard input is empty
stdout
{São Paulo=Osasco}
1253 : {São Paulo=Osasco}
1254 : {São Paulo=Santos}
1255 : {Rio de Janeiro=Cabo Frio}