-6

I was thinking maybe HashMap, but that doesn't allow sorting. Maybe list? I honestly have no clue. I've been struggling with this a lot.

Strings: "Tom", "Sarah", "Oliver". Values: 323, 237.2, 12.1

Dirkx Senne
  • 95
  • 1
  • 3
  • 14
  • What correlation, if any, exists between your data sets? – Makoto May 17 '17 at 17:52
  • 2
    How do you know which value corresponds to which string? Hint: perhaps you should create a class that holds a string and its "value". You could even make that class comparable... – azurefrog May 17 '17 at 17:52
  • Once you have class that holds a name and value, one option is to use [Collections.sort( List, Comparable](https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#sort-java.util.List-java.util.Comparator), with a Comparable that you implement to consider the values. – Andy Thomas May 17 '17 at 17:53

1 Answers1

0

You can use entries with Key and Value like you would do in a hash table but add that entries to a Priority Queue that must be created with the comparator you want.

Here is an example: How do I use a PriorityQueue?

In that case the comparator is created to compare string lengths but you can change that to compare the value of an entry.

The difference is that you have to create the PriorityQueue as new PriorityQueue> instead of new PriorityQueue

Hopes it helps

Community
  • 1
  • 1
Lenin
  • 500
  • 3
  • 11