Loading
Java SortMapDescending

Java Quick Notes

Refresh Your Java - Before Java Interview

We are Agile, believe in less Documentation - Only Quick notes (Java Interview Questions) of Java/J2ee Read more....


Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet
Not Included Yet

Go to SiteMap

Q)  Java SortMap Descending


Ans)

Java SortMap Descending :

public class SortMapDescending {

    public static <K, V> Map<K, V> sortByValue(Map<K, V> map) {
        final int size = map.size();
        final List<Map.Entry<K, V>> meView = new ArrayList<Map.Entry<K,V>>(size);
        meView.addAll(map.entrySet());
        Collections.sort(meView, new DescendingValueComparator());
        Map<K,V> sortedMap = new HashMap();
        for (int i = 0; i < size; i++) {
            sortedMap.put(meView.get(i).getKey(), map.get(meView.get(i).getKey()));
        }
        return sortedMap;
    }
  }

import java.math.BigDecimal;
import java.util.Comparator;
import java.util.Map;

final class DescendingValueComparator<V extends Comparable<? super V>>
                                     implements Comparator<Map.Entry<?, V>> {
    public int compare(Map.Entry<?, V> o1, Map.Entry<?, V> o2) {
        return o2.getValue().compareTo(o1.getValue());
    }
}



Back to top

------------------------- We hope you got necessary Info On -----------------------------------------

Java SortMapDescending


Java SortMapDescending

-------------------------------------------------------------------------------------------------------



Face Book
Request for a Mock Interview/Training

Get a PDF

Face Book
Same look (Read) on any device, this is Ads free