In this Java tutorial, you will learn How to Find Maximum Occurrence of Words from given Text File?
Here is a logic for getting top element:
- Create a class
CrunchifyComparable
that can store the String value of the word and the number of occurrences it appears. - Implement the
Comparable interface
for this class tosort by occurrences first and then alphabetically
if the number of occurrences is the same crunchifyFindMaxOccurance
method, you create a new List ofCrunchifyComparable
from your original map. You add the entries of this to your List- Sort this list
- Take the n-first items of this list using subList
- Add Strings to the
List<String>
and you return it
Another most read
: Find more information on equals() and hashcode()
Java Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
package crunchify.com.tutorial; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Set; public class CrunchifyFindMaxOccurance { /** * @author Crunchify.com */ public static void main(String[] args) throws FileNotFoundException, IOException { File file = new File("/Users/<username>/Documents/test.txt"); BufferedReader bufferedReader = null; bufferedReader = new BufferedReader(new FileReader(file)); String inputLine = null; Map<String, Integer> crunchifyMap = new HashMap<>(); try { while ((inputLine = bufferedReader.readLine()) != null) { String[] words = inputLine.split("[ \n\t\r.,;:!?(){}]"); for (int counter = 0; counter < words.length; counter++) { String key = words[counter].toLowerCase(); // remove .toLowerCase for Case Sensitive result. if (key.length() > 0) { if (crunchifyMap.get(key) == null) { crunchifyMap.put(key, 1); } else { int value = crunchifyMap.get(key).intValue(); value++; crunchifyMap.put(key, value); } } } } Set<Map.Entry<String, Integer>> entrySet = crunchifyMap.entrySet(); System.out.println("Words" + "\t\t" + "# of Occurances"); for (Map.Entry<String, Integer> entry : entrySet) { System.out.println(entry.getKey() + "\t\t" + entry.getValue()); } List<String> myTopOccurrence = crunchifyFindMaxOccurance(crunchifyMap, 1); System.out.println("\nMaixmum Occurance of Word in file: "); for (String result : myTopOccurrence) { System.out.println("==> " + result); } } catch (IOException error) { System.out.println("Invalid File"); } finally { bufferedReader.close(); } } /** * @param map * = All Words in map * @param n * = How many top elements you want to print? If n=1 it will print highest occurrence word. If n=2 it * will print top 2 highest occurrence words. * @returns list of String */ public static List<String> crunchifyFindMaxOccurance(Map<String, Integer> map, int n) { List<CrunchifyComparable> l = new ArrayList<>(); for (Map.Entry<String, Integer> entry : map.entrySet()) l.add(new CrunchifyComparable(entry.getKey(), entry.getValue())); Collections.sort(l); List<String> list = new ArrayList<>(); for (CrunchifyComparable w : l.subList(0, n)) list.add(w.wordFromFile + ":" + w.numberOfOccurrence); return list; } } class CrunchifyComparable implements Comparable<CrunchifyComparable> { public String wordFromFile; public int numberOfOccurrence; public CrunchifyComparable(String wordFromFile, int numberOfOccurrence) { super(); this.wordFromFile = wordFromFile; this.numberOfOccurrence = numberOfOccurrence; } @Override public int compareTo(CrunchifyComparable arg0) { int crunchifyCompare = Integer.compare(arg0.numberOfOccurrence, this.numberOfOccurrence); return crunchifyCompare != 0 ? crunchifyCompare : wordFromFile.compareTo(arg0.wordFromFile); } @Override public int hashCode() { final int uniqueNumber = 19; int crunchifyResult = 9; crunchifyResult = uniqueNumber * crunchifyResult + numberOfOccurrence; crunchifyResult = uniqueNumber * crunchifyResult + ((wordFromFile == null) ? 0 : wordFromFile.hashCode()); return crunchifyResult; } @Override public boolean equals(Object crunchifyObj) { if (this == crunchifyObj) return true; if (crunchifyObj == null) return false; if (getClass() != crunchifyObj.getClass()) return false; CrunchifyComparable other = (CrunchifyComparable) crunchifyObj; if (numberOfOccurrence != other.numberOfOccurrence) return false; if (wordFromFile == null) { if (other.wordFromFile != null) return false; } else if (!wordFromFile.equals(other.wordFromFile)) return false; return true; } } |
Example1 file:
1 2 3 4 5 6 7 8 9 10 |
This is test This is test This is test This is test This is test This is test This is test This is test This test This |
Output:
1 2 3 4 5 6 7 |
Words # of Occurances test 9 this 10 is 8 Maixmum Occurance of Word in file: ==> this:10 |
Example2 File:
1 |
this and and and then and this |
Output:
1 2 3 4 5 6 7 |
Words # of Occurances and 4 this 2 then 1 Maixmum Occurance of Word in file: ==> and:4 |
Are you looking for more java tutorials? Look at this collection.