Using a map to store 1 ID for N values?

Asked

Viewed 66 times

0

EDIT for lack of details, I have answered the question here. The question has been flagged and a moderator will delete it as soon as possible, thank you.

I have the following Ids and values respectively:

1 - 18;
1 - 19;
1 - 20;
3 - 21;
3 - 22;
8 - 23;
8 -24;
8 - 25;
11 - 26;
3 - 27;
3 - 28;

How can I make it so that when I pass the ID value via parameter, it returns the values of this ID? For example, if I passed ID 3, it would return me the values 21, 22, 27 & 28.

I should wear a map <integer, List<Integer>>, correct? But how can I do that? As also I would add these values within my List?

  • Diego, how and where you are writing these values?

  • These values come from the local Sqlite database. The ID (first value) refers to a product, and the value (second value) of a calculation. So for example, ID 3 is a guitar that has 4 different calculations, with Ids 21, 22, 27 & 28

  • then, when the user clicks on the product of ID 3, I have to check which are the calculations that are connected with it. here’s the problem. I think the map this way is the easiest way, but I could be wrong!

  • Instead of loading all the data and putting it into one map you should fetch them from the bank only when the user chooses the ID, bringing the lines that have this ID.

2 answers

1

You can use Hashmap. Do the following:

  HashMap<Integer,ArrayList<Integer>> map=new HashMap<Integer,ArrayList<Integer>>();  
      //  chave = 3  ,  {1,22,27,28}
   map.put(3, new ArrayList<Integer>(Arrays.asList(1,22,27,28)));
   System.out.println( "Valores : " + map.get(3));
   // Resultado = "Valores : [1, 22, 27, 28]"

Where 3 is the key, just change the key and start a new Integer Arraylist

  • Thank you very much for the answer, good friend! But unfortunately my question is a little more complex, I would need to do this with all the values on the list. Like I would apply a for that logic?

  • For example, as long as the ID was 3, it would add in the key 3 arraylist. Then when it went to ID 11, it would add the values in the ID 11 array, but what if it went back to 3, and so on?

  • What structure is this list stored? Why not sort it by ID?

  • Another thing Voce can do too, is read the whole file and generate the array list, and only then put in Hashmap

  • So, man, here it is. I’m getting to display each value in a print, like, the way I sent it up there, I’m just breaking down to store it. I’ll study the gentleman’s logic down there and see if I can get something

  • Friend, thank you for the answer but my question was very badly worded and I still can not find a solution. I will delete it and reformulate.

Show 1 more comment

0

Follow the logic to manage the map, to fetch the value just use the get by passing the id. Any questions about the code just ask.

Map<integer, List<Integer>> map  = new HashMap<integer, List<Integer>>();

public void adicionar(Integer id, Integer elemento){
  if(map.containsKey(id)){
     map.get(id).add(elemento);
  }else{
     List<Integer>> lista = new ArrayList();
     lista.add(elemento);
     map.put(id,lista);
  }
}
  • Friend, thank you for the answer but my question was very badly worded and I still can not find a solution. I will delete it and reformulate.

Browser other questions tagged

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