gl
package com.javarush.task.task18.task1821;
import java.io.*;
import java.util.*;
/*
Встречаемость символов
*/
public class Solution {
public static void main(String[] args) {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(args[0])))) {
String line;
List<Character> list = new ArrayList<>();
while ((line = reader.readLine()) != null){
char[] cr = line.toCharArray();
for (int i = 0; i < cr.length; i++) {
list.add(cr[i]);
}
}
List<Integer> casts = new ArrayList<>();
for (char cr : list){
casts.add((int) cr);
}
casts.sort((x,y) -> x.compareTo(y));
list.clear();
for (int i : casts){
list.add((char)i);
}
int count = 1;
int position = 1;
HashMap<Character, Integer> map = new LinkedHashMap<>();
for (int i = 0; i < list.size() - 1; i++) {
if (list.get(i).equals(list.get(position))){
count++;
map.put(list.get(i), count);
if (position < list.size() - 1) {
position++;
}
} else {
count = 1;
map.put(list.get(position), count);
if (position < list.size() - 1) {
position++;
}
}
}
System.out.println(list);
map.forEach((x,y) -> System.out.println(x + " " + y));
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}