/* TestSet.java CIS 160 David Klick 2009-12-01 Demonstration of HashSet class. */ import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class TestSet { public static void main(String[] args) { Set words = new HashSet(); Scanner in = new Scanner(System.in); String strIn = ""; do { System.out.print("Enter a word (ENTER to exit): "); strIn = in.nextLine(); if (strIn.length() > 0) { if (words.add(strIn)) { System.out.println(strIn + " added to set"); } else { System.out.println("Duplicate detected: " + strIn + " not added to set"); } } } while (strIn.length() > 0); System.out.println("\nWords in set:"); for (String s : words) System.out.println(s); } } /* Sample run: Enter a word (ENTER to exit): hi hi added to set Enter a word (ENTER to exit): there there added to set Enter a word (ENTER to exit): how how added to set Enter a word (ENTER to exit): are are added to set Enter a word (ENTER to exit): you you added to set Enter a word (ENTER to exit): hi Duplicate detected: hi not added to set Enter a word (ENTER to exit): there Duplicate detected: there not added to set Enter a word (ENTER to exit): Words in set: hi how are there you */