Java Map Sort sortEntries(Map map, Comparator> comparator)

Here you can find the source of sortEntries(Map map, Comparator> comparator)

Description

sort Entries

License

Open Source License

Declaration

public static <K, V> LinkedHashMap<K, V> sortEntries(Map<K, V> map, Comparator<Entry<K, V>> comparator) 

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;
import java.util.Map.Entry;

public class Main {
    public static <K, V> LinkedHashMap<K, V> sortEntries(Map<K, V> map, Comparator<Entry<K, V>> comparator) {
        LinkedHashMap<K, V> returnMap = new LinkedHashMap<K, V>();
        List<Entry<K, V>> entries = new LinkedList<Entry<K, V>>(map.entrySet());
        Collections.sort(entries, comparator);
        for (Entry<K, V> entry : entries) {
            returnMap.put(entry.getKey(), entry.getValue());
        }//from  w  ww.j  av  a  2 s .  co  m
        return returnMap;
    }

    public static <K, V, L extends LinkedHashMap<K, V>> L sortEntries(L map, Comparator<Entry<K, V>> comparator) {
        @SuppressWarnings("unchecked")
        L returnMap = (L) map.clone();
        returnMap.clear();
        List<Entry<K, V>> entries = new LinkedList<Entry<K, V>>(map.entrySet());
        Collections.sort(entries, comparator);
        for (Entry<K, V> entry : entries) {
            returnMap.put(entry.getKey(), entry.getValue());
        }
        return returnMap;
    }

    public static <K extends Comparable<K>, V> LinkedHashMap<K, V> sort(Map<K, V> map) {
        LinkedHashMap<K, V> returnMap = new LinkedHashMap<K, V>();
        List<K> keys = new LinkedList<K>(map.keySet());
        Collections.sort(keys);
        for (K key : keys) {
            returnMap.put(key, map.get(key));
        }
        return returnMap;
    }

    public static <K, V> LinkedHashMap<K, V> sort(Map<K, V> map, Comparator<K> comparator) {
        LinkedHashMap<K, V> returnMap = new LinkedHashMap<K, V>();
        List<K> keys = new LinkedList<K>(map.keySet());
        Collections.sort(keys, comparator);
        for (K key : keys) {
            returnMap.put(key, map.get(key));
        }
        return returnMap;
    }

    public static <K extends Comparable<K>, V, L extends LinkedHashMap<K, V>> L sort(L map) {
        @SuppressWarnings("unchecked")
        L returnMap = (L) map.clone();
        returnMap.clear();
        List<K> keys = new LinkedList<K>(map.keySet());
        Collections.sort(keys);
        for (K key : keys) {
            returnMap.put(key, map.get(key));
        }
        return returnMap;
    }

    public static <K, V, L extends LinkedHashMap<K, V>> L sort(L map, Comparator<K> comparator) {
        @SuppressWarnings("unchecked")
        L returnMap = (L) map.clone();
        returnMap.clear();
        List<K> keys = new LinkedList<K>(map.keySet());
        Collections.sort(keys, comparator);
        for (K key : keys) {
            returnMap.put(key, map.get(key));
        }
        return returnMap;
    }
}

Related

  1. sortedMap(Map map, Comparator comparator)
  2. sortedString(Map c)
  3. sortedString(Map c)
  4. sortedTable(Map map)
  5. sortedView(Map original, int numItems)
  6. sortHashMapByValuesD(HashMap passedMap)
  7. sortingHelperSessions(String column, Map sortParams)
  8. sortKeysByValue(final Map m)
  9. sortKeysDesc(HashMap> hash)