Receive 2 parameters when entering a key parameter

Asked

Viewed 188 times

3

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

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

I wonder if there is some way in list, or in some type of class specifies where I just enter the code I need and receive the 2 values (strings) related to it.

I’ve thought of some ways that this might work, but I’m afraid they’re more gambiarras, so I’d like several opinions.

  • Has any response helped solve the problem and can address similar questions from other users? If so, make sure to mark the answer as accepted. To do this just click on the left side of it (below the indicator of up and down votes).

2 answers

3

One of the possible solutions is to work with a Hashmap, that receives as key 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 exit will be:

{Sao paulo=Osasco}

Can be seen working on IDEONE


You can also crack a class by itself, which is responsible for manipulating this HashMap. I made an example as basic as possible of what this class might look like, just to illustrate what the construction would be like:

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;
    }
}

Its 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 on IDEONE.

1

One way is to use the class Multimap library Guava google.

The use 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 Maven you can add project dependency like this:

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

Or lower the . jar here.

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

  • Who negative can say why? If there is something wrong I can correct in the answer

  • I’m not the one who denied it. To tell the truth, I assembled my answer based on what I read in Soen, and I even saw this solution, but I didn’t add in the answer, it was too complex, I preferred to focus only on Hashmap.

  • What’s the difference of this ListMultimap for a HashMap? In the context of the question.

  • @The difference is that this structure was designed precisely for what he wants, which is a key with several items inside. It is a Hashmap of lists, without needing a new implementation

  • @diegofm I imagined that you had not been yourself, quiet, I just wanted to know what was the reason to improve the answer if she has something really wrong, since in my case when I needed something like this I thought it served as a glove

Browser other questions tagged

You are not signed in. Login or sign up in order to post.