Java HashMap Sort sortedScoreMap(HashMap unSortedMap)

Here you can find the source of sortedScoreMap(HashMap unSortedMap)

Description

this function takes unsorted map as input and returns a sorted map

License

Open Source License

Parameter

Parameter Description
unSortedMap a parameter

Declaration

public static LinkedHashMap<Long, Double> sortedScoreMap(HashMap<Long, Double> unSortedMap) 

Method Source Code


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

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;

import java.util.LinkedHashMap;
import java.util.LinkedList;

import java.util.Map.Entry;

public class Main {
    /** this function takes unsorted map as input and returns a sorted map
     * @param unSortedMap//from w  ww  . j  ava2  s . c o  m
     * @return
     */
    public static LinkedHashMap<Long, Double> sortedScoreMap(HashMap<Long, Double> unSortedMap) {

        LinkedList<Entry<Long, Double>> mapList = new LinkedList<Entry<Long, Double>>(unSortedMap.entrySet());
        Collections.sort(mapList, new Comparator<Entry<Long, Double>>() {

            @Override
            public int compare(Entry<Long, Double> o1, Entry<Long, Double> o2) {
                // TODO Auto-generated method stub
                return o1.getValue().compareTo(o2.getValue());
            }

        });

        LinkedHashMap<Long, Double> sortedMap = new LinkedHashMap<Long, Double>();

        for (Entry<Long, Double> entrySet : mapList) {
            sortedMap.put(entrySet.getKey(), entrySet.getValue());
        }

        return sortedMap;

    }
}

Related

  1. getSortedColors(HashMap colors)
  2. getSortedHashMapKeyset(Map sorting)
  3. getSortedLinkedHashMap(Map bitCounts, Comparator comparator)
  4. sortByComparator(HashMap unsortMap, final boolean ascendingorder)
  5. sortByValue(HashMap map)
  6. sortHashMapByKeys(HashMap passedMap, boolean ascending)
  7. sortHashMapByValues( HashMap> passedMap, String key0)
  8. sortHashMapByValues(HashMap passedMap, boolean ascending)
  9. sortHashMapByValues(HashMap passedMap)

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