Reputation: 111
I have a problem, probably because of my inexperience about HashMaps. Basically, I have a class that contains two HashMap variables:
private Map <String, List<String>> definiciones = new <String, List<String>> HashMap();
private Map <String, List<String>> sinonimos = new <String, List<String>> HashMap();
They store Strings in their Lists when I call their respective functions. The problem is that, for example, when I add a String to the List inside definiciones Map, it also appears somehow in sinonimos.
public void agregarDefinicionAPalabra(String palabra, String definicion) throws PalabraInvalida {
System.out.println(definiciones.get(palabra).size());
if(definicion.equals("")) {
JOptionPane.showMessageDialog(null, "La definición no puede estar vacía");
throw new PalabraInvalida("La definición no puede estar vacía");
}
if (definiciones.containsKey(palabra)) {
definiciones.get(palabra).add(definicion);
} else {
throw new PalabraInvalida("No se ha encontrado la palabra solicitada");
}
System.out.println(sinonimos.get(palabra).get(0));
}
The method adds a String to the definition List, as you can see, in the end I put a print to confirm this idea that I just wrote. Indeed, when I run this method, the sinonimos List gets the definiciones element that I added. Obviously there is something that I don´t know due to my inexperience in this topic, any suggestions?
Thank you!
Upvotes: 1
Views: 500
Reputation: 338730
The other two Answers are correct. I'll add this graphic to make visual the problem and solution.
Or if you want the second list to be based on contents of the first, make a copy. To make a copy, feed the first list to the constructor of the second list.
sinonimos.set( palabra , new ArrayList<>( firstArrayList ) ) ;
An entirely new and separate list is created. But the elements in both lists point to the same content objects. So be aware of those element objects being mutable or immutable.
Upvotes: 1
Reputation: 1742
You probably put the same list object into both of your HashMaps. In Java, all objects are referenced by their memory address. When you put a list into your HashMap, it's really just storing the memory address of the list.
As an analogy, if I gave the street address of my home to two friends, and both of them wrote down my address, I don't suddenly get two houses. If one of my friends leaves a package on my doorstep, and then the other friend comes by, they will see the package still there.
So, in the code that is creating the Lists, make sure you create distinct, separate lists for the two Maps.
Upvotes: 2
Reputation: 2502
I think you must be using the same List
object for the value in the definiciones
and sinonimos
maps. For example, you must be doing something like:
String palabara = "example";
List<String> list = new ArrayList<>();
definiciones.set(palabra, list);
sinonimos.set(palabra, list);
What happens here is that you now have the same list as the value in both maps. So when you call definiciones.get(palabra)
and sinonimos.get(palabra)
, both of these will give you references to the same list in memory.
What you need to do instead is:
String palabara = "example";
definiciones.set(palabra, new ArrayList<>());
sinonimos.set(palabra, new ArrayList<>());
Upvotes: 1