More than a sorting with dupplicate elimination
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
My Problem:
Have Quizz and a Part of the Programm show the User some Feedback.
I built some "dummy" Names, where the Number of answered Questions is updatet randomly, so the User think that some other users are around
Now my Problem i want something like this after sorting:
14 Questions: Mike, Anna
12 Questions: Michael, John
11 Questions: Andreas, Nick
But i get something like this:
14 Questions: Mike
14 Questions: Anna
12 Questions: Michael
12 Questions: John
....
Tried with hashSet and TreeMap but nothing worked
... hope
My Code:
I try first to sort array against array with hashSet:
did not work
Then use a TreeMap:
Then took put the Numbers n1 ... n10 and names in a TreeMap:
Problem no dupplicates but lost names:
instead of:
14 Questions: Mike, Anna
12 Questions: Michael, John
i got:
14 Questions: Anna
12 Questions: John
.. no dupplicates, but lost names ...
If any ideas ... BIG THANKS!!! ... if hard to understand what my Problem(s) are, i will try to post once more
Have Quizz and a Part of the Programm show the User some Feedback.
I built some "dummy" Names, where the Number of answered Questions is updatet randomly, so the User think that some other users are around
Now my Problem i want something like this after sorting:
14 Questions: Mike, Anna
12 Questions: Michael, John
11 Questions: Andreas, Nick
But i get something like this:
14 Questions: Mike
14 Questions: Anna
12 Questions: Michael
12 Questions: John
....
Tried with hashSet and TreeMap but nothing worked
... hope My Code:
I try first to sort array against array with hashSet:
did not work
Then use a TreeMap:
Then took put the Numbers n1 ... n10 and names in a TreeMap:
Problem no dupplicates but lost names:
instead of:
14 Questions: Mike, Anna
12 Questions: Michael, John
i got:
14 Questions: Anna
12 Questions: John
.. no dupplicates, but lost names ...
If any ideas ... BIG THANKS!!! ... if hard to understand what my Problem(s) are, i will try to post once more
posted 14 years ago
Welcome to JavaRanch.
Using a Map was a good idea.
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:
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
Welcome to JavaRanch.
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:
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
@Jesper, Thank You !
That works very fine, one problem solved new one occurs
Now my question, it´s the TreeMap the RIGHT tool to do this, or should i try something else ?
If i put the names like suggested, i got something like this:
14 Ana, Mike, ..
15 John, Travis ...
Give for each name one variable, c1 (Ana), c11 (Mike), c2 (John), c21 (Travis) where at begin they have the same values c1 = c11 = 14, c2 = c21 = 15.
Sort with TreeMap, no problem.
Now, i increase each of them randomly, exeample:
now when two of my variables have the same value, my initial problem appears again
means after first round random, c1 = 18, c11 = 19, c2 = 19, c21 = 22
now i see:
18 Ana
19 John <------------------------------- Mike is missing here
22 Travis
Why i need this? In my quiz ( here the old version without the TreeMap modifications QUIZ ) i give out a updated list with the "dummy" users + the real user.
For each dummy user i give a variable that increase randomly, so the one user who play have the impresion that 10-15 other guys play at the same time with him
.
That works very fine, one problem solved new one occurs
Now my question, it´s the TreeMap the RIGHT tool to do this, or should i try something else ?
If i put the names like suggested, i got something like this:
14 Ana, Mike, ..
15 John, Travis ...
Give for each name one variable, c1 (Ana), c11 (Mike), c2 (John), c21 (Travis) where at begin they have the same values c1 = c11 = 14, c2 = c21 = 15.
Sort with TreeMap, no problem.
Now, i increase each of them randomly, exeample:
now when two of my variables have the same value, my initial problem appears again
means after first round random, c1 = 18, c11 = 19, c2 = 19, c21 = 22
now i see:
18 Ana
19 John <------------------------------- Mike is missing here
22 Travis
Why i need this? In my quiz ( here the old version without the TreeMap modifications QUIZ ) i give out a updated list with the "dummy" users + the real user.
For each dummy user i give a variable that increase randomly, so the one user who play have the impresion that 10-15 other guys play at the same time with him
. posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
In a Map, each key can only have one value. For key 19 you want two values, but that's simply not possible with maps. Unless you use a Map<X,List<Y>>, but it will require some more work.
SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6 - OCEJPAD 6
How To Ask Questions How To Answer Questions
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
@ Rob
if i get it right, when i have a Map i can use two different keys with the same value?
In the Map If i have the keys = my variable c1, c11, c2, c21, ... c6, c61 and the names "John, Mike ... ".
when the value of my two keys is the same c1 = c11 = 19 ... in TreeMap i dont have key 1 value John and key 2 value Mike, key 2 override my key 1?
i allready got the hint:
and it works on the first time, at new sorting again if two keys the same show me one name....
Your idea something like that? I tried so, but the same, only one name per integer
if i get it right, when i have a Map i can use two different keys with the same value?
In the Map If i have the keys = my variable c1, c11, c2, c21, ... c6, c61 and the names "John, Mike ... ".
when the value of my two keys is the same c1 = c11 = 19 ... in TreeMap i dont have key 1 value John and key 2 value Mike, key 2 override my key 1?
i allready got the hint:
and it works on the first time, at new sorting again if two keys the same show me one name....
Your idea something like that? I tried so, but the same, only one name per integer
posted 14 years ago
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?
-
1 -
-
Number of slices to send:Optional 'thank-you' note:
-
-
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?
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Thanks Jesper!
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 .
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 . posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
First, Thank you for all the help!
Now it works well, the Names are sorted in the right oder.
Have 2 more Questions, when i try to give the result in a window, it dont looks so nice:
1 QUESTION
:
how can i remoe the Strings so i get something like this:
1 NameX
2 NameY
3 NameZ
2 QUESTION
:
does treeMap provide a descending sorting:
3 NameZ
2 NameY
1 NameX
Do i need that my class implements the NavigableMap<K,V> interface ? ... after 6 hours of coding, searching ... looks that my brain got a buffer overflow
Now it works well, the Names are sorted in the right oder.
Have 2 more Questions, when i try to give the result in a window, it dont looks so nice:
1 QUESTION
:
how can i remoe the Strings so i get something like this:
1 NameX
2 NameY
3 NameZ
2 QUESTION
: does treeMap provide a descending sorting:
3 NameZ
2 NameY
1 NameX
Do i need that my class implements the NavigableMap<K,V> interface ? ... after 6 hours of coding, searching ... looks that my brain got a buffer overflow
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Reverse Order was easier as i tought
SortedMap<Integer,ArrayList<String>>Sortierte_liste = new TreeMap<Integer,ArrayList<String>>(Collections.reverseOrder());
instead of
SortedMap<Integer,ArrayList<String>>Sortierte_liste = new TreeMap<Integer,ArrayList<String>>();
sometime the solution is simpler as we think
SortedMap<Integer,ArrayList<String>>Sortierte_liste = new TreeMap<Integer,ArrayList<String>>(Collections.reverseOrder());
instead of
SortedMap<Integer,ArrayList<String>>Sortierte_liste = new TreeMap<Integer,ArrayList<String>>();
sometime the solution is simpler as we think
posted 14 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Resolved Problem, Code here:
https://coderanch.com/t/526309/java/java/Problems-printing-Treemap-nd-run
https://coderanch.com/t/526309/java/java/Problems-printing-Treemap-nd-run
| I will suppress my every urge. But not this shameless plug: The new gardening playing cards kickstarter is now live! https://www.kickstarter.com/projects/paulwheaton/garden-cards |









