Find most repeated word in a File - Java Program

Q. Write a program to find the most repeated word in a text file in Java.

Answer:

In this example, we check the repeated word from the given text file. The name of the text file is “welcome.txt”. The Map class is used as a generic to get text file. Use a counter in a loop which counts each word in given file.

RepeatedWord.java

import java.io.*;
import java.util.*;
import java.util.Map.Entry;
import java.util.regex.Pattern;

public class RepeatedWord
{
     public static void main(String args[])
     {
          Map<String, Integer> wordMap = buildWordMap("welcome.txt");
          List<Entry<String, Integer>> list = sortByValueInDecreasingOrder(wordMap);
          System.out.println("List of repeated word from file and their count");
          for (Map.Entry<String, Integer> entry : list)
          {
               if (entry.getValue() > 1)
               {
                    System.out.println(entry.getKey() + " => " + entry.getValue());
               }
          }
     }
     public static Map<String, Integer> buildWordMap(String fileName)
     {
          Map<String, Integer> wordMap = new HashMap<>();
          try
          (FileInputStream fis = new FileInputStream(fileName);
          DataInputStream dis = new DataInputStream(fis);
          BufferedReader br = new BufferedReader(new InputStreamReader(dis)))
          {
               Pattern pattern = Pattern.compile("\\s+");
               String line = null; while ((line = br.readLine()) != null)
               {
                    line = line.toLowerCase();
                    String[] words = pattern.split(line);
                    for (String word : words)
                    {
                         if (wordMap.containsKey(word))
                         {
                              wordMap.put(word, (wordMap.get(word) + 1));
                         }
                         else
                         {
                              wordMap.put(word, 1);
                         }
                    }
               }
          }
          catch (IOException ioex)
          {
               ioex.printStackTrace();
          }
          return wordMap;
      }
     public static List<Entry<String, Integer>> sortByValueInDecreasingOrder(Map<String, Integer> wordMap)
     {
          Set<Entry<String, Integer>> entries = wordMap.entrySet();
          List<Entry<String, Integer>> list = new ArrayList<>(entries);
          Collections.sort(list, new Comparator<Map.Entry<String, Integer>>()
          {
               @Override
               public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2)
               {
                    return (o2.getValue()).compareTo(o1.getValue());
               }
          });
          return list;
     }
}


Output:

repeated word