jainilvachhani
jainilvachhani

Reputation: 904

Reverse a nested TreeMap in Java

I have a TreeMap of the form

TreeMap<String, TreeMap<String, Integer>> map = new TreeMap<>();

which contains a roll_no, subject, and marks. I want to sort students by their marks and hence need to reverse the TreeMap and make it of the form:

TreeMap <TreeMap<Integer, String>, String> reverseMap = new TreeMap<>(Collections.reverseOrder());

Upvotes: 0

Views: 755

Answers (2)

Shubham Kadlag
Shubham Kadlag

Reputation: 2308

This should work. Also gave an example. Please tell if any issues.

    public static void main(String[] args) {

        TreeMap<String, TreeMap<String, Integer>> map = new TreeMap<>();
        TreeMap <TreeMap<Integer,String>, String> reverseMap = new TreeMap<>(Collections.reverseOrder((o1,o2) -> 1));
        TreeMap<String,Integer> r1=new TreeMap<>();
        r1.put("English", 80);
        r1.put("Maths", 90);
        r1.put("Science", 75);
        r1.put("Biology", 50);


        TreeMap<String,Integer> r2=new TreeMap<>();
        r2.put("English", 50);
        r2.put("Maths", 60);
        r2.put("Science", 80);
        r2.put("Biology", 70);

        map.put("1", r1);
        map.put("2", r2);


        map.entrySet().stream().sorted((obj1,obj2)->{
            if(obj1.getValue().values().stream().mapToInt(Integer::intValue).sum() > obj2.getValue().values().stream().mapToInt(Integer::intValue).sum()) return 1;
            else if(obj1.getValue().values().stream().mapToInt(Integer::intValue).sum() < obj2.getValue().values().stream().mapToInt(Integer::intValue).sum()) return -1;
            return 0;
        }).collect(Collectors.toSet()).forEach(t-> {
            TreeMap<Integer,String> newmap=new TreeMap<>(Collections.reverseOrder());
            t.getValue().forEach((k,v)-> newmap.put(v, k));
            reverseMap.put(newmap, t.getKey());             
        });

        reverseMap.entrySet().forEach(t-> {System.out.println("Roll no: "+t.getValue());t.getKey().entrySet().forEach(a-> System.out.println(a.getKey()+" "+a.getValue()));
        });


    }

Upvotes: 1

AxelH
AxelH

Reputation: 14572

Why not simply reverse the comparator from the start by passing a specific comparator instance to the TreeMap constructor :

Map<String, Integer> map = new TreeMap<>(Collections.reverseOrder());
map.put("foo", 1);
map.put("bar", 2);
map.put("doe", 3);

System.out.println(map);

{foo=1, doe=3, bar=2}

Of course, withtout the reversed comparator :

{bar=2, doe=3, foo=1}

That solution will not required two maps (the original and the reversed one).

If you want both, then use putAll with a TreeMap with a reverse comparator :

Map<String, Integer> reversedMap = new TreeMap<>(Collections.reverseOrder());
reversedMap.putAll(map);

System.out.println(reversedMap);

Note that I completly ignore your data structure since this isn't appropriate so I kept my design simple to show what can be done. A Map of Map isn't a good idea.

Upvotes: 2

Related Questions