Home > Blockchain >  Word list search with hashmap in Java
Word list search with hashmap in Java

Time:05-31

  public ArrayList<String> wordReader () throws FileNotFoundException {
        File txt = new File(path);
        Scanner scanner = new Scanner(txt);
        ArrayList <String> words = new ArrayList<String>();
        while (scanner.hasNextLine()) {
            String data = scanner.nextLine();
            words.add(data);
        }
        scanner.close();
        return words;
    }

I have a word list and there are more than 50,000 words in my word list. As you can see above, I read my words and add them to an Array List, but after this process, when I want to read my words, it happens very slowly. That's why Hashmap came to my mind. I want to read my words and when I receive a word input from the user, I want to have it checked whether it is in the hashmap. Even though I did research, I could not find how to do it exactly. How can I do this.

CodePudding user response:

Since you will be checking whether the word input is present in your list of words read from the file, you can use a HashSet<String> instead of using an ArrayList<String>.

Your method would then become

public HashSet<String> wordReader () throws FileNotFoundException {
        File txt = new File(path);
        Scanner scanner = new Scanner(txt);
        HashSet <String> words = new HashSet<String>();
        while (scanner.hasNextLine()) {
            String data = scanner.nextLine();
            words.add(data);
        }
        scanner.close();
        return words;
    }

Now after you read the word input, you can check whether it is present in the HashSet. This would be a much faster operation as lookup would take constant time.

public boolean isWordPresent(String word, HashMap<String> words){
    return words.contains(word);
}

As a side note, HashSet internally uses a HashMap to perform the operations.

CodePudding user response:

I would use a Set, not a List since sets automatically ignore duplicates when you add them to the set. If it wasn't present it returns true and adds it, otherwise false.

public Set<String> wordReader () throws FileNotFoundException {
        File txt = new File(path);
        Scanner scanner = new Scanner(txt);
        Set <String> words = new HashSet<>();
        while (scanner.hasNextLine()) {
            String data = scanner.nextLine();
            if(!words.add(data)) {
               // present - Do something
            } 
         }   
        
        scanner.close();
        return words;
}
  • because sets are not ordered they are not random access collections. So you can add the set to a list as follows:
Set<String> words = wordReader();
List<String> wordList = new ArrayList<>(words);

Now you can retrieve them with an index.

  • you may want to make your method more versatile by passing the file name as an argument.
  • Related