• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Aron Jhed Amiscosa
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
when your children are suffering from your punishment, tell your them it will help them write good poetry when they are older. Like this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic