So I am trying to put a string of words as a key into a map, and a set of strings as a value into the map. Person1 is just a string of words, and Person2 is a set.
Map<String, Set<String>> newMap = new TreeMap<String, Set<String>>();
Set<String> newSet = new TreeSet<String>();
newSet.add(person2);
map.put(person1, newSet);
//System.out.println(map);
So person1 is:
Apples
Apples
Pears
Oranges
Apples
and person 2 is:
[Love]
[Like]
[Dislike]
[Hate]
[OK]
When I put person1 and person2 into the map and run the program, Java updates the value of the key (Apples could be Love, Like and OK, but since the last value is OK, it set it to OK)
{Apples = [OK]}
{Pears = [Dislike]}
{Oranges = [Hate]}
What I want is this:
{Apples = [Love, Like, OK]}
{Pears = [Dislike]}
{Oranges = [Hate]}
Is this possible? If so, how?
You need to retrieve the Set already associated with the key and add the new word to that set. Only if the key does not have a Set already should you create a new Set.