Nick Gilbert
Nick Gilbert

Reputation: 4240

Moving data from a Hash Table and Sorting it

I'm writing a program that counts word occurances in a plaintext file. To do this, I use a hash table. After I get all the words and how many times they appear I want to print them in alphabetical order. To do that, I need to pull the Pairs out of the Hash Table and sort then in an array of pairs based on each word. I'm using a merge sort for this:

/**
     * 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;

And this is the code in my main method to call the sort and print the sorted data

        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());
            }
        }

At the end where I iterate through the sortedData and print it's key and value, it all prints as if it had never been sorted and I can't figure out why.

Upvotes: 0

Views: 243

Answers (1)

Kartic
Kartic

Reputation: 2985

I guess you can use TreeMap to keep it simple :

Create a method to add to a TreeMap like :

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);
    }
}

Now use this method :

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

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

To print the values you can do :

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;
}

Upvotes: 1

Related Questions