// CSE 373, Winter 2013, Marty Stepp // This program demonstrates the use of Java's Set collection. // The program originally used a List, but a Set is faster for this example. import java.io.*; import java.util.*; public class Lecture02 { public static void main(String[] args) throws FileNotFoundException { Set words = new HashSet(); Scanner input = new Scanner(new File("bible.txt")); while (input.hasNext()) { String word = input.next(); // don't need to call contains because Set already avoids duplicates // if (!words.contains(word)) { words.add(word); // } } System.out.println("There are " + words.size() + " unique words."); // use a "for-each" loop to loop over the elements of a Set for (String w : words) { System.out.println(w); } } }