Java Map Sort sort(T map, Comparator> c)

Here you can find the source of sort(T map, Comparator> c)

Description

sort

License

Open Source License

Declaration

public static <K, V, T extends Map<K, V>> T sort(T map, Comparator<Map.Entry<K, V>> c) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

public class Main {
    public static <K, V, T extends Map<K, V>> T sort(T map, Comparator<Map.Entry<K, V>> c) {
        List<Map.Entry<K, V>> list = new LinkedList<>(map.entrySet());
        Collections.sort(list, c);
        Object result = new LinkedHashMap<>();
        for (Map.Entry<K, V> entry : list) {
            ((T) result).put(entry.getKey(), entry.getValue());
        }/*w ww  .  j  ava2s .com*/
        return (T) result;
    }
}

Related

  1. sort(Map map)
  2. sort(Map map, Comparator> comparator)
  3. sort(Map map)
  4. sort(Map src)
  5. sortBy(Collection collection, final Map valuesMap)
  6. sortByComparator(Map unsortMap)
  7. sortByComparator(Map unsortMap)
  8. sortByComparatorDecreasing(Map unsortMap)