Java Map Sort sortKeysDesc(HashMap> hash)

Here you can find the source of sortKeysDesc(HashMap> hash)

Description

Sort the integer keys in a HashMap from highest to lowest.

License

Open Source License

Parameter

Parameter Description
hash The hashmap of type Integer, Vector<String>.

Declaration

public static int[] sortKeysDesc(HashMap<Integer, Collection<String>> hash) 

Method Source Code


//package com.java2s;
/*/*from  w  ww.j  a  v a 2 s . co m*/
com.rivescript.RiveScript - The Official Java RiveScript Interpreter
Copyright (C) 2010  Noah Petherbridge
    
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;

public class Main {
    /**
     * Sort the integer keys in a HashMap from highest to lowest.
     *
     * @param hash The hashmap of type Integer, Vector<String>.
     */
    public static int[] sortKeysDesc(HashMap<Integer, Collection<String>> hash) {
        // Make a vector of all the number-keys of the hash.
        Collection<Integer> keys = new ArrayList<Integer>();

        // Get all the keys.
        Iterator it = hash.keySet().iterator();
        while (it.hasNext()) {
            int wc = Integer.parseInt(it.next().toString());
            keys.add(wc);
        }

        // Turn the key vector into an int array.
        int[] iKeys = Iv2s(keys);

        // Order the keys in descending order.
        Arrays.sort(iKeys);

        // Reverse and return it!
        int[] reversed = new int[iKeys.length];
        int k = 0;
        for (int j = iKeys.length - 1; j >= 0; j--) {
            reversed[k] = iKeys[j];
            k++;
        }

        return reversed;
    }

    /**
     * Convert an int vector into an int array.
     *
     * @param vector The vector to convert.
     */
    public static int[] Iv2s(Collection<Integer> vector) {
        int[] result = new int[vector.size()];
        int i = 0;
        Iterator it = vector.iterator();
        while (it.hasNext()) {
            result[i] = Integer.parseInt(it.next().toString());
            i++;
        }
        return result;
    }
}

Related

  1. sortedView(Map original, int numItems)
  2. sortEntries(Map map, Comparator> comparator)
  3. sortHashMapByValuesD(HashMap passedMap)
  4. sortingHelperSessions(String column, Map sortParams)
  5. sortKeysByValue(final Map m)
  6. sortKeyValuePairByValue( Map map)
  7. sortMap(final Map map, final Comparator> comparator)
  8. sortMap(LinkedHashMap map, Comparator> c)
  9. sortMap(Map oldMap)