I am not sure if i did everything right in this task, I tried to accomplish it using HashMap.
How can i print results, if i have already maxEntry of my map?
Thank you for any piece of advice :)
package pl.codegym.task.task18.task1803;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
/*
Najczęściej występujące bajty
*/
public class Solution {
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String fileName = reader.readLine();
FileInputStream inputStream = new FileInputStream(fileName);
Map<Integer, Integer> map = new HashMap();
while (inputStream.available() > 0){
int temp = inputStream.read();
if (!map.containsKey(temp)){
map.put(temp, 1);
} else {
Integer value = map.get((temp));
map.put(temp, value + 1);
}
}
reader.close();
inputStream.close();
Map.Entry<Integer, Integer> maxEntry = null;
for (Entry<Integer, Integer> entry : map.entrySet()) {
if (maxEntry == null || entry.getValue() > maxEntry.getValue()){
maxEntry = entry;
}
}
// not sure how to properly display results
for (Map.Entry<Integer, Integer> entry : map.entrySet()){
if (entry.equals(maxEntry)){
System.out.print(entry.getKey());
System.out.print(" ");
}
}
}
}