Receive 2 parameters when entering a key parameter

3

I have a code that is related to 2 values, for example:

1253 (code), São Paulo (first value, in the case the state), and Osasco (second value, in this case, the city).

I would like to know if there is any way in a list, or some kind of specific class where I just enter the code I need and receive the 2 strings related to it.

I've thought of some ways this might work, but I'm afraid they're more of a gambiar, so I'd like a variety of opinions.

    
asked by anonymous 26.11.2016 / 20:14

2 answers

3

One of the possible workarounds is to work with a HashMap , which receives the key as the code, and other HashMap as value, where you will save the state-city pair.

public class MapTest {

    public static void main(String[] args) {
    //map principal
    Map<Integer, HashMap<String, String>> codeStateList = new HashMap<>();  
    //cria um map para cruzar estado cidade
    HashMap<String, String> stateCity = new HashMap<>();
    //adiciona o par estado cidade
    stateCity.put("Sao paulo", "Osasco");
    //salva no map principal o code cruzando com map com par estado-cidade
    codeStateList.put(1253, stateCity);
    //para exibir ou pegar o par
    System.out.println(codeStateList.get(1253));
    }
}

The output will be:

  

{Sao paulo = Osasco}

It can be seen working in IDEONE

You can also create a separate class, which is responsible for manipulating this HashMap . I made as basic an example as possible of what this class could be, just to illustrate how the construction would be:

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();) {
            Map.Entry<Integer, HashMap<String, String>> entry = iterator.next();
            if (entry.getKey().equals(key)) {
                iterator.remove();
            }
        }
    }

    public boolean containsKey(int key) {
        return codeStateList.containsKey(key);
    }

    public String showAllItens() {
        String allItens = "";
        for (Map.Entry<Integer, HashMap<String, String>> entry : codeStateList.entrySet()) {
            allItens += entry.getKey() + " : " + entry.getValue() + "\n";
        }
        return allItens;
    }
}

Your use:

public 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());

    }
}

See working at IDEONE .

    
26.11.2016 / 21:58
1

One way is for you to use the Multimap class % of Google's %% Google

library.

The usage would be as follows:

ListMultimap<Integer, String> cidades = ArrayListMultimap.create();

cidades.put(1253, "São Paulo");
cidades.put(1253, "Osasco");

System.out.println(cidades.get(1253));

If you are using Guava you can add the dependency of the project like this:

<dependency>
  <groupId>com.google.guava</groupId>
  <artifactId>guava</artifactId>
  <version>20.0</version>
</dependency>

Or download .jar here .

You can get more explanations about the latest release (20);

    
26.11.2016 / 23:31