0

I have created a Hash map where I need to store two different values corresponding to one key. Using

final Map<String, List<Double>> map = new HashMap<String, List<Double>>();
final List<Double> valSetOne = new ArrayList<Double>();
for(int i=0;i<100;i++)
{
    valSetOne.add(movies.getPrice());
    valSetOne.add(movies.getSeat());
    map.put(movies.getId, valSetOne);
}
System.out.println(map);

On printing the value of map I'm getting output something like this{1235567=[1100.00,100], 1256689=[1300.00,100]}

Now I want to sort this hash map in descending order on the basis of price i.e key and values of 1300.00 should come before 1100.00.

choasia
  • 10,404
  • 6
  • 40
  • 61
Jhon
  • 27
  • 4
  • 2
    `HashMap` doesn't store its values in order. It doesn't even maintain order of insertion. So you will have to look to other data structures to keep the sorted data. – denvercoder9 Dec 08 '16 at 08:41
  • 1) You have the same list object as value for each key 2) you can't sort a hash, do you mean a list? – fafl Dec 08 '16 at 08:42
  • It's not possible to sort a HashMap. Consider using TreeMap, lookup higherKey() / higherEntry() and lowerKey() / lowerEntry() functions. Consider implementing a comparator for Movie class and store that directly. – PentiumPro200 Dec 08 '16 at 08:44
  • 1
    You probably want a `TreeMap>`, which will sort the entries in alphabetical order of the key, and sort the elements within each value in numerical order. – Dawood ibn Kareem Dec 08 '16 at 08:49
  • check https://www.mkyong.com/java/how-to-sort-a-map-in-java/ – Roshan Dec 08 '16 at 09:14
  • Possible duplicate of [Sort a Map by values (Java)](http://stackoverflow.com/questions/109383/sort-a-mapkey-value-by-values-java) – David Rawson Dec 08 '16 at 09:16

1 Answers1

0

Considering that you are building your HashMap<String, List<Double>> correctly with each entry having String parameter representing your movieId as key and a List<Double> parameter as value which contains movies.getPrice() as it's first entry and movies.getSeat() as it's second entry, you can create a TreeMap<String, List<Double>> with a Comparator and add all the entries of your old map to it in order to get a sorted version of your old map based on the movie price. TreeMap construction can be done like this:

Java 8 way:

TreeMap<String, List<Double>> priceSortedMap = new TreeMap<>((o1, o2) ->{
            //get first entry (movie price) of list and compare.
            if(map.get(o1).get(0) <= map.get(o2).get(0)){
                return 1;
            }
            return -1;
        });
 // put all entries from old map to new         
   priceSortedMap.putAll(map);

Here o1 and o2 are the String keys.

Java 7 way:

TreeMap<String, List<Double>> priceSortedMap = new TreeMap<>(new Comparator<String>() {

            @Override
            public int compare(String o1, String o2) {
                //get first entry (movie price) of list and compare.
                if(map.get(o1).get(0) <= map.get(o2).get(0)){
                    return 1;
                }
                return -1;
            }

        });
 // put all entries from old map to new         
       priceSortedMap.putAll(map);

I hope this is what you were trying to achieve.

Shyam Baitmangalkar
  • 1,075
  • 12
  • 18