ΠΡΠΎΠ΄Π΅ Π΄Π°Π½Π½ΠΎΠ΅ ΡΠ΅ΡΠ΅Π½ΠΈΠ΅ ΠΏΠΎ ΠΌΠ°ΠΊΡΠΈΠΌΠ°Π»ΡΠ½ΠΎ Π²ΡΡΡΠ΅ΡΠ°ΡΡΠ΅ΠΌΡ Π±Π°ΠΉΡΡ ΠΏΡΠΎΡ
ΠΎΠ΄ΠΈΠ»ΠΎ ΡΠ°Π½Π΅Π΅, ΠΏΠΎΠ΄ΡΠΊΠ°ΠΆΠΈΡΠ΅ ΠΏΠΎΠΆΠ°Π»ΡΠΉΡΡΠ°, ΡΡΠΎ Ρ ΡΠΏΡΡΠΊΠ°Ρ
package com.javarush.task.task18.task1823;
import java.io.*;
import java.util.*;
/*
ΠΠΈΡΠΈ ΠΈ Π±Π°ΠΉΡΡ
*/
public class Solution {
public static Map<String, Integer> resultMap = new HashMap<String, Integer>();
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String name;
while (true) {
name = reader.readLine();
if(!name.equals("exit")){
ReadThread readThread = new ReadThread(name);
readThread.start();
} else {
break;
}
}
}
public static class ReadThread extends Thread {
String filename;
public ReadThread(String fileName) {
//implement constructor body
this.filename=fileName;
}
// implement file reading here - ΡΠ΅Π°Π»ΠΈΠ·ΡΠΉΡΠ΅ ΡΡΠ΅Π½ΠΈΠ΅ ΠΈΠ· ΡΠ°ΠΉΠ»Π° ΡΡΡ
public void run () {
HashMap<Integer, Integer> list = new HashMap<>();
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(filename);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while (true) {
try {
if (!(inputStream.available()>0)) break;
} catch (IOException e) {
e.printStackTrace();
break;
}
int num = 0;
try {
num = inputStream.read();
} catch (IOException e) {
e.printStackTrace();
}
if (list.containsKey(num)) {
int value = list.get(num);
list.put(num, value + 1);
}
else {
list.put(num,1);
}
}
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
List<Integer> value = new ArrayList<>(list.values());
Collections.sort(value);
Integer max = value.get(list.size()-1); // Π±Π΅ΡΠ΅ΠΌ ΠΏΠ΅ΡΠ²ΡΠΉ ΡΠ»Π΅ΠΌΠ΅Π½Ρ ΡΠΏΠΈΡΠΊΠ°, ΡΡΠΎ ΠΌΠΈΠ½ΠΈΠΌΠ°Π»ΡΠ½ΠΎΠ΅ Π·Π½Π°ΡΠ΅Π½ΠΈΠ΅
for (Map.Entry<Integer, Integer> pair : list.entrySet()) {
Integer b = pair.getValue();
if (b.equals(max)) {
resultMap.put(filename, b);
}
}
}
}
}