HashMap vs. HashSet
HashSetDemo1.java reads a file of the 50 states into a HashSet. Afterwards a file names queries is read which contains some words that may or may not be states. Each word read from the queries file is a search key into the HashSet. Query words found in the map are reported as being actual states. Queriy words not found in the set are reported as not being actual names of states.
- input files used by the HashSetDemo1 program: AllStates.txt and queries.txt
- download the above three files to your desktop. Compile the java file and execute it. No command args needed.
HashMapDemo1.java reads a file where each line is a state followed by its capitol. Each pair is stored into a HashMap of String to String. The state is the key and the capitol is stored alongside the state into the HashMap. Afterwards the queries.txt file is read again. This time as each token is read from the queries file, that token is searched for in the Keys column of the HashSet. if it is found, the capitol of that state is retreived and printed from the map.
- input files used by the HashMapDemo1 program: States2Capitols.txt and the queries.txt file above
- download the above two files to your desktop. Compile the java file and execute it. No command args needed.
- Note the use of the .split() method that is built in the String class. Split allows you to break up a string into an array of substrings by choosing a character or characters to delimit tokens. The expression "\\s+" is a regular expression that means any chunk of 1 or more contiguous whitespace characters is treated as a single delimiter.