Reputation: 496
I am new to Hashtables, and am trying to understand how they fully work. I need to determine if a string occurs in a large file that contains around 100,000 strings, each on their own line. I have been told that a HashTable would be much more efficient than a LinkedList or ArrayList to runtime being O(n) for both.
I have looked into the HashTable Class in Java, but do not understand how exactly I would enter in every string from the file due to the "put" method needing a key, as well as the object.
I imagine I could use a Scanner to run through every string in my file, but how would I enter them into a Hashtable, and how would the contains() method be utilized in a HashTable?
Upvotes: 0
Views: 285
Reputation: 4111
You need a HashSet to store each line of your file.
*You would probably need a HashMap only if you were interested in the number of occurrences of each String in the file.
import java.io.File;
import java.io.FileNotFoundException;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class MyFileUtils {
//this can be omitted, just added to increase speed
//when requiring multiple searches in the same file, to avoid recalculations.
//Use it only if you need to search in one file ONLY
private static Set<String> stringLines = null;
/*
* Get a HashSet of all the (distinct) lines in a file
*/
public static Set<String> getStringLinesFromFile (String filePath) {
//this can be omitted, just added to support fast multiple calls of this function
if (stringLines != null) return stringLines;
Set<String> stringLines = new HashSet<String>();
Scanner scanner = null;
try {
scanner = new Scanner(new File(filePath));
while (scanner.hasNextLine())
stringLines.add(scanner.nextLine());
} catch (FileNotFoundException e) {
System.out.println("File does not exist");
} finally {
if(scanner != null)
scanner.close();
}
//as the first line, this can be omitted, just added to support fast multiple calls of this function
MyFileUtils.stringLines = stringLines;
return stringLines;
}
/*
* Call this method to search for a stringLine in a file
*/
public static boolean checkIfStringExistsInFile(String filePath, String aStringLine) {
return getStringLinesFromFile(filePath).contains(aStringLine);
}
//Test
public static void main (String args[]) {
System.out.println(checkIfStringExistsInFile("test.txt", "Hello World"));
}
}
Upvotes: 1
Reputation: 5451
You could just put the strings in to a HashSet
.
Set yourStrings = new HashSet<String>();
for (String line : yourFile) {
yourStrings.add(line);
}
Then to check if a particular string is present:
if (yourStrings.contains("Hi!")) {
// It's present
}
else {
// It's not present
}
Upvotes: 1
Reputation: 31699
Hashtable
is pretty old, and has been superseded by HashMap
. There's also HashSet
. Both use a hash table, but they have different purposes. A HashMap
is used when you want to associate some sort of value with each key; for example, you could use it to look up somebody's name and get their phone number. A HashSet
, however, just stores the keys without any values. You'd use that just to add names to the set, and then later check to see if the name is in the set.
As Luiggi mentioned in the comments, HashMap
and HashSet
are just specific implementations of Map
and Set
; those implementations use hash tables, but other implementations of those classes are available. You'll need to use HashMap
and HashSet
when constructing the table, but you should usually declare your variables simply as Map
and Set
, because that way you could replace the HashMap
or HashSet
with some other class that implements the same methods. That way you're not tied to a specific implementation.
Upvotes: 1
Reputation: 3120
To me, this sounds like a Usecase of HashSet
. Check that out!
You put only values in, which are stored in a distinct way (no doubled values). You can then just call the contains
method, to check wether your String is inside the Set.
Upvotes: 3