4

I have a language dictionary (i.e. english,italian,etc...), that essentially is a file with one word on every line.

Now i want to create a class with a method that given a string in input check if that string exists into that dictionary.

My idea is that the method return a boolean value. In pseudocode:

boolean checkWord(String s){
    if(StringIsInDictionary) return true;
    return false
}

What should be the best way to implement that feature?

Consider that the file will contain ~65000 words.

1
  • Oli actually i haven't tried nothing. Commented Mar 2, 2013 at 15:40

4 Answers 4

7

Read the dictionary into a Set<String> (for example, HashSet<String>), and then use set.contains(word).

Sign up to request clarification or add additional context in comments.

2 Comments

And consider using a HashSet constructor which takes an initialCapacity parameter. docs.oracle.com/javase/7/docs/api/java/util/…
Is it efficient also for smartphones?
2

For a space and time efficent solution (like you might use on a smartphone), consider a bloom filter. Then you won't need to store the dictionary on the phone, and checking that a string is in a dictionary will be very fast. Note that a bloom filter may return a false positive, but you can tune it to reduce that risk.

There are several open-source Java implementations of bloom filters out there. One is here https://github.com/magnuss/java-bloomfilter.

1 Comment

+1, A Bloom filter is optimal for situations where memory and performance are constrained.
1

You will probably not want to store the words as one word per line. A better approach might be to read the file from disk only once, store the words in a HashSet (a set backed by a HashMap, which is very efficient to search), and then use set.contains("mystring"). This will, however, require the whole map to be in memory, but it will be very efficient when you need to check multiple words.

You could then even go back and serialize the set in a more efficient manner to disk, making the initial loading faster.

Comments

1

Take a look at this question, I think it could help you. Fastest way to find a string in a text file with java

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.