Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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);
    }
}
share|improve this question
    
Is it not already exactly correct? If that's the case, it shouldn't be here. – QPaysTaxes Dec 24 '15 at 15:24
up vote 3 down vote accepted

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)
}
share|improve this answer
1  
or replace get and put lines in java 8 by: wordCount.merge(word, 1, Math::addExact); – Ritesh Dec 24 '15 at 15:36

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.

share|improve this answer

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.

share|improve this answer
    
Welcome to Code Review and good job on your first answer! – SirPython Dec 26 '15 at 22:59

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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