<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">import java.io.*;
import java.util.*;

public class WordList {
    
    /* TreeSet extends SortedSet and maintains a sorted binary tree (no duplicates) of
       elements.  Since Collection implements Serializable, TreeSet is serializable. */
    static private SortedSet words = new TreeSet();
    static private int totalWords = 0;
    static private int distinctWords = 0;

    public static void main(String[] args) throws IOException, ClassNotFoundException {

	// Read the file (named in command line argument) into the Sorted Set
	StreamTokenizer in = new StreamTokenizer(new FileReader(args[0]));
	in.resetSyntax();
	in.wordChars('a', 'z');  // treat letters as part of words and nothing else
	in.wordChars('A', 'Z');
	in.whitespaceChars(0, 'A'-1);
	in.whitespaceChars('Z'+1, 'a'-1);
	in.whitespaceChars('z'+1, 255);
	in.lowerCaseMode(true);  // convert word tokens to lower case automatically
	while (in.nextToken() != StreamTokenizer.TT_EOF) {
	    if (in.ttype == StreamTokenizer.TT_WORD) {
		++totalWords;
		words.add(in.sval);  // add word to collection
	    }
	}
	
	// Write the StringSet to a file and Read it Back in;
	File temp = new File("temp.out");
	temp.deleteOnExit();  // delete this file when the program terminates
	ObjectOutputStream tOut = new ObjectOutputStream(new FileOutputStream(temp));
	tOut.writeObject(words);
	tOut.close();
	
	ObjectInputStream tIn = new ObjectInputStream(new FileInputStream(temp));
	SortedSet recoveredWords = (SortedSet)tIn.readObject();
	tIn.close();
	
	// Print the words to the display in sorted order
	Iterator it = recoveredWords.iterator();
	while (it.hasNext()) {
	    ++distinctWords;
	    System.out.println((String)it.next());
	}

	System.out.println("\nUnique Word Ratio: " + 
			   (double)distinctWords / (double)totalWords);
    }
}
	
	
    
</pre></body></html>