The program below demonstrates use of a map to count the frequency of words in a file, where a word is delimited by the regular expression [^a-zA-Z']+.
The following classes and interface are used in the program:
The class Scanner can parse primitive types and strings using regular expressions.
A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace.
The resulting tokens may then be converted into values of different types using the various next methods.
The interface Map<K,V> maps keys to values where K is the type of keys maintained by this map and V is the type of mapped values.
The class TreeMap<K,V> is sorted according to the natural ordering of its keys.
WordCounter.java (a simple word counter)
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
public class WordCounter {
public static void main( String[] args ) {
try {
File f = new File( args[0] );
Scanner sc;
sc = new Scanner( f );
// sc.useDelimiter( "[^a-zA-Z']+" );
Map<String, Integer> wordCount = new TreeMap<String, Integer>( );
while ( sc.hasNext( ) ) {
String word = sc.next( );
if( !wordCount.containsKey( word ) )
wordCount.put( word, 1 );
else
wordCount.put( word, .get( word ) + 1 );
}
// Show results.
for ( String word : wordCount.keySet( ) )
System.out.println( word + " " + wordCount.get( word ) );
System.out.println( wordCount.size( ) );
}
catch( IOException e ) {
System.out.println( "Unable to read from file." );
}
}
}