Add more than one identical key to a table

Asked

Viewed 33 times

-1

A problem arose in an excerpt of the code, where it is stored in a variable Hashmap 2 different values, the problem is that the indexing as I understand it is done by the key, that is, it is not possible to have duplicated key. Follows the excerpt from the code in question:

HashMap contribuinteList = (HashMap) origem.get("ConstribuinteList");

idContribuinte = request.getParameter("idcontribuinte");
exercicio = gb.getYear();

contribuinteList.put(idContribuinte, exercicio);

What would be the best way to work in this situation, given that I can have more than 1 contributor ?

  • I didn’t understand the problem. If you can have more than one contributor, I understand that each one has a different ID, so just add in the map: contribuinteList.put(idContribuinte1, exercicio1), contribuinteList.put(idContribuinte2, exercicio2), etc. I would only change the name to contribuintes, or contribuinteMap, since contribuinteList may give the false impression that it is a list (List, ArrayList, etc), which is another completely different data structure

  • Friend @Victor Henrique each contributor will have their own id, so there is to have duplicate key....

  • It is an import, so I can have "15336,2018" "15336,2019", the id can be repeated. In this case it would update the key, which could not occur, since I need the integrity of the data as entered.

1 answer

0

I decided as follows:

List listaContribuinte = new ArrayList();

HashMap map = new HashMap();

map.put(IdContribuinte,exercicio);

listaContribuinte.add(map);

for( int i = 0; i < listaContribuinte.size(); i++){
   // Percorro linha a linha da lista pegando o map para realizar as ações.
}

Browser other questions tagged

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