Java HashMap Sort getSortedHashMapKeyset(Map sorting)

Here you can find the source of getSortedHashMapKeyset(Map sorting)

Description

get Sorted Hash Map Keyset

License

Open Source License

Declaration

public static <T> List<T> getSortedHashMapKeyset(Map<T, Integer> sorting) 

Method Source Code


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

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Main {
    public static <T> List<T> getSortedHashMapKeyset(Map<T, Integer> sorting) {
        List<T> types = new ArrayList<>();
        Map<T, Integer> clone = new HashMap<>();
        for (T player : sorting.keySet()) {
            clone.put(player, sorting.get(player));
        }/*from  ww  w  .j  ava2 s . com*/
        for (int i = 0; i < sorting.size(); i++) {
            int highestNumber = Integer.MIN_VALUE;
            T highestType = null;
            for (T player : clone.keySet()) {
                if (clone.get(player) > highestNumber) {
                    highestNumber = clone.get(player);
                    highestType = player;
                }
            }
            clone.remove(highestType);
            types.add(highestType);
        }
        return types;
    }
}

Related

  1. getSortedColors(HashMap colors)
  2. getSortedLinkedHashMap(Map bitCounts, Comparator comparator)
  3. sortByComparator(HashMap unsortMap, final boolean ascendingorder)
  4. sortByValue(HashMap map)
  5. sortedScoreMap(HashMap unSortedMap)

  6. HOME | Copyright © www.java2s.com 2016