So I am reading a file and need to count the number of duplicates within that file. I cant store duplicates. I would then need to display the contents of the file based on order of occurrence
My code so far:
// use hashmap to store the values
Map<String, Integer> myMap = new HashMap<>();
// loop through
for (String line = r.readLine(); line!=null; line = r.readLine()) {
if (myMap.containsKey(line)) {
myMap.put(line, myMap.get(line)+1);
} else {
myMap.put(line, 1);
}
}
I am storing them in a map because they have unique keys; the problem I am facing is that I need to sort them by the value of the integer from greatest to least.
Example input:
World
World
World
Hello
Hello
Hello
Hello
Expected output:
Hello
World