4
\$\begingroup\$

I have the following requirements:

/* Collection

Write a program that counts the number of unique words in a large text file.

a] Store the words in a collection and report the number of unique words.

b] Once you have created the collection, allow the user to search it to see whether various words appear in the text file and to get the count if present

*/

I have implemented the following code to satisfy the above requirements. Please review the code and give suggestions to make this code more clear, exactly correct and optimized.

import java.util.*;
import java.io.*;
public class test5
{
    public static void main(String[]args) throws FileNotFoundException
    {

        Scanner s = new Scanner(new File("test.txt"));
        ArrayList<String> wordList = new ArrayList<String>();
        Map<String, Integer> wordCount = new HashMap<String, Integer>();        

        while (s.hasNext())
        {
            wordList.add(s.next());
        }

        HashSet<String> hset = new HashSet<String>(wordList);

        System.out.println("Count of Unique words from file is: "+hset.size());

        for(String word: wordList)
        {
            Integer count = wordCount.get(word);          
            wordCount.put(word, (count==null) ? 1 : count+1);
        }
        System.out.println(wordCount);
    }
}
\$\endgroup\$
  • \$\begingroup\$ Is it not already exactly correct? If that's the case, it shouldn't be here. \$\endgroup\$ – Nic Hartley Dec 24 '15 at 15:24
3
\$\begingroup\$

Your code looks overly complex to me, using three different collections when one, the hash map, would be enough. Use your code from the wordList loop when emptying the scanner, and update the word count there:

while (scanner.hasNext()) {
    String word = scanner.next()
    Integer count = wordCount.get(word)
    wordCount.put(word, count == null ? 1 : ++count)
}
\$\endgroup\$
  • 1
    \$\begingroup\$ or replace get and put lines in java 8 by: wordCount.merge(word, 1, Math::addExact); \$\endgroup\$ – Ritesh Dec 24 '15 at 15:36
2
\$\begingroup\$

One small piece of advice: Use the generic interface (i.e. Map or Set) instead of the implementation, unless you absolutely need a method that only exists in the implementation. It's a good habit to get into so that, in production code, if you decide that you want to use another kind of List or whatever, it's easier to switch, and you don't have to worry about having accidentally used a method specific to the implementation. Also, it makes it clear that you just need a Map from String to Integer, not specifically a map that works by hashing its keys and putting them in buckets.

Also, another small piece of advice: Use containsKey() instead of checking if the result of get() is null.

\$\endgroup\$
2
\$\begingroup\$

Seems like you forgot to close your Scanner object. In Java 1.7 and up, you can do it as simple as wrapping it in a "try-with-resources", like this:

try(Scanner s = new Scanner(new File("test.txt")))
{
    // using s 
}

On prior versions, you should use the finally clause. See here for more info.

\$\endgroup\$
  • \$\begingroup\$ Welcome to Code Review and good job on your first answer! \$\endgroup\$ – SirPython Dec 26 '15 at 22:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.