Returns a sorted entry set of given map. - Java java.util

Java examples for java.util:Map Value

Description

Returns a sorted entry set of given map.

Demo Code


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{
    /**/*w  w  w . j  a v a 2  s  .c om*/
     * Returns a sorted entry set of given map.
     * 
     * @param <K>
     *            key type
     * @param <V>
     *            value type
     * @param map
     *            Map to be sorted
     * @return Sorted entry set
     */
    public synchronized static <K, V extends Comparable<V>> List<Entry<K, V>> sortByValue(
            Map<K, V> map) {
        List<Entry<K, V>> entries = new ArrayList<Entry<K, V>>(
                map.entrySet());
        Collections.sort(entries, new ByValue<K, V>());
        return entries;
    }
}

Related Tutorials