-4

I have two HashMaps:

Map<String,Integer> map1 = {"One"= 1245598 , "two" = 0 ,"three" =1};
Map<String,Boolean> map2 = {"One" =true , "two" =false ,"three" = true};

where "one" ,"two" are attributes.

I have sorted the first map easily which is map<String,Integer>. But how can I sort my second map based on the first map, so that the sort method will return me a map which will look like:

map2= {"One" =true , "three" = true ,"two" = false};

i.e. in descending order.

  • How did you sort a `HashMap`? They are unordered, so that makes no sense. What was your sorting order for map1? How is `One`, `three`, `two` "descending"? They look rather unordered to me. – Andreas Jul 20 '16 at 06:52
  • What is the meaning of "sort my second map based on first map " based on keys, values or something different? – ead Jul 20 '16 at 06:54

2 Answers2

1

Maps are inherently unsorted. The fact that the first map is sorted is merely by chance. E.g.

Map<String, Integer> map1 = new HashMap<>();
map1.put("Four", 4);
map1.put("One", 1245598);
map1.put("two", 0);
map1.put("three", 1);
map1.put("Six", 6);

will result in

map1={Six=6, Four=4, One=1245598, two=0, three=1}

To get a sorted map, you need to use a special map type, such as TreeMap. You can specify a comparator for TreeMap, which allows you to sort the second Map based on the values of the first map.

Map<String, Integer> map1 = new TreeMap<>();
map1.put("One", 1245598);
map1.put("two", 0);
map1.put("three", 1);

Map<String,Boolean> map2 = new TreeMap<>(Comparator.comparing(map1::get).reversed());
map2.put("One",true);
map2.put("two", false);
map2.put("three", true);

System.out.println("map1="+map1);
System.out.println("map2="+map2);

Output:

map1={One=1245598, three=1, two=0}
map2={One=true, three=true, two=false}

Note however that updates to the first map may break the second one.


Comparator.comparing(map1::get).reversed()

is just a short way to create a comparator in java 8:

Comparator.comparing(func)

Creates a Comparator that compares 2 objects by comparing the results of applying func to those objects, i.e. the value returned from the compare(o1, o2) method of this Comparator is

func.apply(o1).compareTo(func.apply(o2))

map1::get is a method reference to the get method of map1. Given a value o1 it will return map1.get(o1). Combining those 2 facts the return type of the comparator created therefore is

map1.get(o1).compareTo(map1.get(o2))

comparator.reversed() just returns a Comparator for the reversed order for descending order instead of ascending sorting.

fabian
  • 80,457
  • 12
  • 86
  • 114
  • how u are using comparing(map::get).Can u pls explain it properly – Sukirti Shukla Jul 20 '16 at 08:22
  • @SukirtiShukla: Added some explanation. If you're not familiar with method references, you may want to read the oracle tutorial about them: https://docs.oracle.com/javase/tutorial/java/javaOO/methodreferences.html – fabian Jul 20 '16 at 08:43
0

Instead of using HashMap use a TreeMap. That will sort your maps.

You can refer these links: LINK 1 or link 2

Map<String, Integer> treeMap1 = new TreeMap<String, Integer>(
            new Comparator<String>() {

            @Override
            public int compare(String o1, String o2) {
                return o2.compareTo(o1);
            }

        });
        treeMap1.putAll(map1);

Similarly, create a second treeMap with <String, Boolean>

Community
  • 1
  • 1
R.K
  • 1,721
  • 17
  • 22
  • Do let me know if you have any doubts – R.K Jul 20 '16 at 07:01
  • u r telling to sort both the map independently.But how can i sort boolean map based on map of integer sort map.I dont understand clearly. – Sukirti Shukla Jul 20 '16 at 07:44
  • Treemap here will sort the map according to the keys.. Since both the maps have same set of keys, their sorted order will be same. You don't have to sort map2 as per map1, u have to individually sort both the maps in same order.... This is done by tremap comparator – R.K Jul 20 '16 at 07:54
  • Do you have any confusions? – R.K Jul 20 '16 at 07:57
  • I have to sort boolean map according to integer map since this is the need that Integer map will sort in descending order of there values and after that i will sort the same in boolean map.I cant compare on "string" keys even if keys are same in both maps – Sukirti Shukla Jul 20 '16 at 08:14
  • Firstly, get it clear that you are not sorting your map in descending order. Secondly, you want it finally something like map1 ["one"=1, "three"=3, "two"=2] and map2 ["one"=true, "three"="true", "two"="false"]. At last, what you want is both of them to be in same order of keys (By the way, this is ascending order of keys).. This is exactly what treeMap will do for you. – R.K Jul 20 '16 at 10:25