0

I am currently writing a word counter program which will use a Hashtable to count the words in a file and I would like to create a linked list within the program to sort the words' occurrence in descending order.

I know how to add elements to a linked list but I don't know how to add elements from a Hashtable to a linked list and sort the values in descending order. Can you please help with that?

Here is the code I have so far:

import java.io.FileReader;
import java.util.*;
import java.util.Hashtable;
import java.util.stream.Collectors;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class WordCounter {

  public Hashtable count_words(String contents) {
    Hashtable < String, Integer > count = new Hashtable < String, Integer > ();

    Set < String > key = count.keySet();

    StringTokenizer w = new StringTokenizer(contents);

    while (w.hasMoreTokens()) {
      String word = w.nextToken();

      word = word.toLowerCase();
      word = word.replaceAll("[-+.^:(\"),']", "");

      if (count.containsKey(word)) {
        count.put(word, count.get(word) + 1);
      } else {
        count.put(word, 1);
      }
    }
    return count;
  }


  public LinkedList top20(Hashtable count) {
    ///I don't know how to add elements from hashtable to linkedlist
    return new LinkedList();
  }


  public static void main(String args[]) {
    try {
      String contents = "";
      Scanner in = new Scanner(new FileReader("src/ADayInTheLife.txt"));
      while ( in .hasNextLine()) {
        contents += in .nextLine() + "\n";
      }
      WordCounter wc = new WordCounter();
      Hashtable count = wc.count_words(contents);

      System.out.println(count);

    } catch (Exception e) {
      System.err.println("Error " + e.getMessage());
    }
  }
}
Generic Bot
  • 309
  • 1
  • 4
  • 8
pharaphoks
  • 51
  • 9
  • It would make more sense to use an `ArrayList`, as you already know how many items there are, and you can sort that with `Collections.sort()`. – user207421 Aug 17 '17 at 05:13

2 Answers2

1

Here are high level steps 1) Define a LinkNode with attributes word and count and self reference to LinkNode as next 2) Define a method that will return reference to the head of LinkNode 3) In the method iterate hash table and perform below activities a) if the linkList is empty Create a LinkNode with word and count values and assign this as head of the LinkList b) else You need to find the place where the new node to be inserted (traverse the nodes and compare the count with the nodes on the list to decide based on your order) 3) you can return the constructed head node

1

One possible solution is to use lambda, in this example there is no need to convert from Hash to LinkedList, just using the Hash to sort and list the first 20 in reverse order:

Try with this approach:

Hashtable<String,Integer> count = wc.count_words(contents);

count.entrySet().stream().sorted(Map.Entry.<String,Integer> comparingByValue().reversed()).limit(20).forEach(System.out::println);
Daniel C.
  • 5,418
  • 3
  • 23
  • 26