Description
In java interview, this program can be asked in a multiple ways such as write program to find max repeated words or duplicate words or the count of each duplicate words.Whatever the question, the main programming concept is the same to count the occurrence of each word in a .txt file. To solve this programatically, we can use Map implmenetation in Java that does not allow any duplicate key and at the end of iteration we can find out the count.Following is the complete program.Here, we are using java 8 Lambda operator during sorting.
MaxRepeatedWord.javapackage com.devglan; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Set; import java.util.StringTokenizer; import java.util.Map.Entry; public class MaxRepeatedWord { public MapgetWordsCount(String fileName){ FileInputStream fis; DataInputStream dis; BufferedReader br = null; Map wordMap = new HashMap (); try { fis = new FileInputStream(fileName); dis = new DataInputStream(fis); br = new BufferedReader(new InputStreamReader(dis)); String line; while((line = br.readLine()) != null){ StringTokenizer st = new StringTokenizer(line, " "); while(st.hasMoreTokens()){ String tmp = st.nextToken().toLowerCase(); if(wordMap.containsKey(tmp)){ wordMap.put(tmp, wordMap.get(tmp)+1); } else { wordMap.put(tmp, 1); } } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally{ try { if (br != null) { br.close(); } } catch(Exception ex){ } } return wordMap; } public List > sortByValue(Map wordMap){ Set > set = wordMap.entrySet(); List > list = new ArrayList >(set); Collections.sort(list, (o1, o2) -> (o2.getValue()).compareTo( o1.getValue() )); return list; } public static void main(String a[]){ MaxRepeatedWord maxRepeatedWord = new MaxRepeatedWord(); Map wordMap = maxRepeatedWord.getWordsCount("C:/test.txt"); List > list = maxRepeatedWord.sortByValue(wordMap); System.out.println("Max repeated word is " + list.get(0).getKey() + " with count - " + list.get(0).getValue()); } }