aspose file tools*
The moose likes Beginning Java and the fly likes hashmap different keys with similar values and Arrays.sort() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "hashmap different keys with similar values and Arrays.sort()" Watch "hashmap different keys with similar values and Arrays.sort()" New topic
Author

hashmap different keys with similar values and Arrays.sort()

Jhedzkie Skies
Ranch Hand

Joined: Oct 28, 2009
Posts: 118
i am trying to work out on a HashMap in order to pair up my values...

so its like this




this piece of code generates the first output i needed...


P1 = 2.0
P2 = 3.0
P3 = 6.0
P4 = 7.0
P5 = 8.0
P6 = 3.0
P7 = 3.0
P8 = 1.0
P9 = 2.0
P10 = 4.0


and some more lines below the code, is the Arrays.sort() to sort the values of p[]



and it gives this output...

2.0 is mapped to P9
1.0 is mapped to P8
2.0 is mapped to P9
3.0 is mapped to P7
3.0 is mapped to P7
3.0 is mapped to P7
4.0 is mapped to P10
6.0 is mapped to P3
7.0 is mapped to P4
8.0 is mapped to P5


the next output must show the sorted values on the left mapped to the same P# as it was assigned above...

but, it seems that when a value is a duplicate, it only shows 1 P# that contains the value and i want
to get the sorted value together with the assigned P#.

can you help me on this one?

i wanted to be my output like


2 is mapped to P1
1 is mapped to P8
2 is mapped to P9
3 is mapped to P2
3 is mapped to P6
3 is mapped to P7
4 is mapped to P10
6 is mapped to P3
7 is mapped to P4
8 is mapped to P5


thanks in advance...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
Difficult to understand the question. You want to get the value on the right and the index (P1, P2, etc) ti is mapped from? The essence of a Map is that it represents a function, which means one key one value. You can't have one key several values; that is not a function. If you put 3 --- P2 then later 2 --- P6 and 2 --- P7, the Map will only contain the last pair, 2 --- P7.

There are several ways round it. You can try mapping from 3 to P2, P6 and P7 by mapping to a List containing P2 P6 and P7. That's probably the easiest way to do it. Remember you would have to try getting the List for a particular key first; if you getnull back, you know you have never put that List in the first place. If you get non-null you know you already have a value there and need to add another value.
Jhedzkie Skies
Ranch Hand

Joined: Oct 28, 2009
Posts: 118
Campbell Ritchie wrote:......The essence of a Map is that it represents a function, which means one key one value. You can't have one key several values; that is not a function. If you put 3 --- P2 then later 2 --- P6 and 2 --- P7, the Map will only contain the last pair, 2 --- P7.


thanks for the information on this.

i'll try to rework my code according to your answer...
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
You're welcome
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: hashmap different keys with similar values and Arrays.sort()