从哈希表移动数据并对其进行排序

时间:2015-02-22 01:31:45

标签: java sorting hashtable

我正在编写一个程序来计算明文文件中的单词出现次数。为此,我使用哈希表。在我得到所有的单词和它们出现的次数后,我想按字母顺序打印它们。为此,我需要从Hash表中拉出Pairs,然后根据每个单词在一对数组中进行排序。我正在使用合并排序:

/**
     * Method to merge sort data pulled from a hash table
     * Based on code found at: http://stackoverflow.com/questions/20795158/sorting-names-using-merge-sort
     * 
     * @param names
     * @return 
     */
    public static HashPair<String, Integer>[] mergeSort(HashPair<String, Integer>[] data)
    {
        HashPair<String, Integer>[] sortedData = null;
        if (data.length >= 2) {
            HashPair<String, Integer>[] left = new HashPair[data.length / 2];
            HashPair<String, Integer>[] right = new HashPair[data.length - data.length / 2];

            for (int i = 0; i < left.length; i++) {
                if(data[i] != null)
                {
                    left[i] = data[i];
                }
            }

            for (int i = 0; i < right.length; i++) {
                if(data[i + data.length / 2] != null)
                {
                    right[i] = data[i + data.length / 2];
                }
            }

            mergeSort(left);
            mergeSort(right);
            sortedData = merge(data, left, right);
        }
        return sortedData;
    }

    /**
     * Helper method to merge the data back into one array
     * Based on code found at: http://stackoverflow.com/questions/20795158/sorting-names-using-merge-sort
     * 
     * @param names
     * @param left
     * @param right
     */
    public static HashPair<String, Integer>[] merge(HashPair<String, Integer>[] data, HashPair<String, Integer>[] left, HashPair<String, Integer>[] right) 
    {
        int a = 0;
        int b = 0;
        for (int i = 0; i < data.length; i++) 
        {
            try
            {
                if (b >= right.length || (a < left.length && left[a].getFirst().compareToIgnoreCase(right[b].getFirst()) < 0)) 
                {
                    data[i] = left[a];
                    a++;
                } 
                else 
                {
                    data[i] = right[b];
                    b++;
                }
            }
            catch(NullPointerException e)
            {
                continue;
            }
        }
        return data;

这是我的main方法中的代码,用于调用排序并打印排序数据

        HashPair<String, Integer>[] unsortedData = wordTable.getData(); //Getting unsorted data to be sorted
        HashPair<String, Integer>[] sortedData = mergeSort(unsortedData);
        System.out.printf("%-20s %-20s %-20s\n", "Index", "Key", "Value");
        for(int i = 0; i < sortedData.length; i++)
        {
            if(sortedData[i] != null)
            {
                System.out.printf("%-20s %-20s %-20s\n", i, sortedData[i].getFirst(), sortedData[i].getSecond());
            }
        }

在我遍历sortedData并打印它的键和值的最后,所有打印都好像它从未被排序过,我无法弄清楚原因。

1 个答案:

答案 0 :(得分:1)

我猜您可以使用TreeMap来保持简单:

创建一个添加到TreeMap的方法,如:

private void addToTreeMap(TreeMap<String, Integer> tm, String str) {
    if(tm.containsKey(str)) {
        tm.put(str, tm.get(str).intValue() + 1);
    }
    else {
        tm.put(str, 1);
    }
}

现在使用此方法:

TreeMap<String, Integer> tm = new TreeMap<String, Integer>();

addToTreeMap(tm, "bb");
addToTreeMap(tm, "aa");
addToTreeMap(tm, "aa");

要打印您可以执行的值:

Set<String> setOfKeys = tm.keySet();
Iterator<String> iterator = setOfKeys.iterator();

while (iterator.hasNext()) {
    String key = iterator.next();
    System.out.println("Word: "+key+", count: "+ tm.get(key));
    key = null;
}