Java Map Sort sortMap(Map map, Comparator> compator)

Here you can find the source of sortMap(Map map, Comparator> compator)

Description

sort Map

License

Apache License

Declaration

public static <K, V> Map<K, V> sortMap(Map<K, V> map, Comparator<Entry<K, V>> compator) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class Main {
    public static <K, V> Map<K, V> sortMap(Map<K, V> map, Comparator<Entry<K, V>> compator) {
        Map<K, V> result = new LinkedHashMap<K, V>();
        List<Entry<K, V>> entries = new ArrayList<Entry<K, V>>(map.entrySet());
        Collections.sort(entries, compator);
        for (Entry<K, V> entry : entries) {
            result.put(entry.getKey(), entry.getValue());
        }//w w w. jav  a  2s.  c  o m

        return result;
    }
}

Related

  1. sortMap(final Map map, final Comparator> comparator)
  2. sortMap(LinkedHashMap map, Comparator> c)
  3. sortMap(Map oldMap)
  4. sortMap(Map oldMap, final boolean asc)
  5. sortMap(Map map, int k)
  6. sortMapByKey(Map data)
  7. sortMapByKey(Map map)
  8. sortMapByValue(Map input, final boolean desc)
  9. sortMapByValue(Map map, Comparator comparator)