Lucian Botezatul wrote:Tried with hashSet and TreeMap but nothing worked ... hope
Using a Map was a good idea.
Lucian Botezatul wrote:
Then use a TreeMap:
Then took put the Numbers n1 ... n10 and names in a TreeMap:
In the first try, why did you use Object (an array of objects) as the key in the map?
In the second try: That was almost working, but the reason it did not is this: When you call put() on a Map, and the Map already contains an element for the key that you are using, then you are replacing the element in the Map. So if there is already an entry 14 -> Mike in the map and you put Anna under key 14, then Mike will be replaced by Anna, so you'll not have Mike in the map anymore.
Instead of just putting Anna in there, you will want to get what is in the map first, and if it is not null, then add Anna to it. Like this:
Lucian Botezatul wrote:if i get it right, when i have a Map i can use two different keys with the same value?
Yes, only the keys have to be unique, for the values it doesn't matter.
But you want to store multiple values under the same key; the key is the number, and the values are the names. You can do that with a Map that looks like this:
But my suggestion was to use a Map that doesn't store a List of Strings as the values, but just a single string - and that one string contains the names separated by commas, for example "Anna, Mike".
With your second piece of code: You almost got it. You already noticed that "Anna" and "Anna 2" are both under the same key. Now, what would you have to do to store "Anna" and "Mike" under the same key?
Now i got the key concept of the TreeMap.
I was thinking all the time that the key is the variable herself and the TreeMap sort after the value of the variable , so i followed that having two keys different keys (c1, c11) with the same value the names comes automaticaly on the same raw .