Skip to content

Instantly share code, notes, and snippets.

@milindjagre
Last active December 17, 2018 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milindjagre/a41d85d0130e9cf101fb9eaad92dd4e6 to your computer and use it in GitHub Desktop.
Save milindjagre/a41d85d0130e9cf101fb9eaad92dd4e6 to your computer and use it in GitHub Desktop.
This method returns the HashMap of words and their respective counts.
Map<String, Integer> wordCountMap = new HashMap<String, Integer>();
String[] inputFilePathArray = new String[4];
inputFilePathArray[0] = "C:\\input1.txt";
inputFilePathArray[1] = "C:\\input2.txt";
inputFilePathArray[2] = "C:\\input3.txt";
inputFilePathArray[3] = "C:\\input4.txt";
for (String inputFilePath : inputFilePathArray) {
BufferedReader br = new BufferedReader(
new FileReader(inputFilePath));
String line = null, mapKey = null;
String[] lineSplitter = null;
int i = 0;
while ((line = br.readLine()) != null) {
lineSplitter = line.split(" ");
for (i = 0; i < lineSplitter.length; i++) {
mapKey = lineSplitter[i].replaceAll("[\\.\\',\\?]", "");
if (!(stopWordsList.contains(mapKey))
&& mapKey.length() > 3) {
if (wordCountMap.containsKey(mapKey))
wordCountMap.put(mapKey,
wordCountMap.get(mapKey) + 1);
else
wordCountMap.put(mapKey, 1);
}
}
}
br.close();
}
LinkedHashMap<String, Integer> sortedWordCountMap = sortHashMapByValues(wordCountMap);
System.out.println("***TOP 10 MOST USED WORDS***");
int count = 0;
for (Entry<String, Integer> entry : sortedWordCountMap.entrySet()) {
if (count < 10)
System.out.println(entry.getKey() + " - " + entry.getValue());
count++;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment