How is it giving me this error? I even changed my output to a set so there shouldn't be any duplicates. Although the map should have solved this already. Thanks
package com.codegym.task.task18.task1803;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.util.Map;
import java.util.HashMap;
import java.util.HashSet;
/*
Most frequent bytes
*/
public class Solution {
public static void main(String[] args) throws Exception {
// create reader
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
// create stream
FileInputStream stream = new FileInputStream(reader.readLine());
// create hashMap
Map<Integer, Integer> map = new HashMap<>();
// create stream while loop
while (stream.available() > 0) {
// use conditional and put key and value into hashmap
Integer key = stream.read();
if (key == null) {
map.put(key, 1);
} else {
Integer value = map.get(key);
map.put(key, ++value);
}
}
// close the stream
stream.close();
// create ArrayList
HashSet<Integer> array = new HashSet<>();
// initialize max
Integer max = 0;
// loop through the map and find bigger values
for (Map.Entry<Integer, Integer> pair : map.entrySet()) {
Integer maxValue = pair.getValue();
if (maxValue > max) max = maxValue;
}
// loop through map and add to array
for (Map.Entry<Integer, Integer> pair : map.entrySet()) {
if (pair.getValue() == max) array.add(pair.getKey());
}
// print out max
for (Integer mostFrequent : array) {
System.out.print(mostFrequent + " ");
}
}
}