sort Map Descending - Java java.util

Java examples for java.util:Map Operation

Description

sort Map Descending

Demo Code

//package com.java2s;

import java.util.ArrayList;

import java.util.Collections;
import java.util.Comparator;

import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class Main {
    public static <T extends Comparable> List<Entry<String, T>> sortMapDesc(
            Map<String, T> keywordMap) {
        List<Entry<String, T>> arrayList = new ArrayList<Entry<String, T>>(
                keywordMap.entrySet());//w  w  w.j a  v a2 s  .com
        Collections.sort(arrayList, new Comparator<Entry<String, T>>() {

            @Override
            public int compare(Entry<String, T> e1, Entry<String, T> e2) {
                return (e2.getValue()).compareTo(e1.getValue());
            }
        });
        return arrayList;
    }
}

Related Tutorials