package p13_pliki.tadeusz;

import java.io.File;
import java.io.FileNotFoundException;
import java.text.Collator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.stream.Collectors;

public class PoliczWszystkieSlowa5 {

	public static void main(String[] args) {
		try(Scanner scanner = new Scanner(new File("pan_tadeusz.txt"))) {
			scanner.useDelimiter("[^\\p{L}\\d]+");
			
			scanner.tokens()
				.map(String::toLowerCase)
				.collect(Collectors.groupingBy(x -> x, Collectors.counting()))
				.entrySet()
				.stream()
				.sorted(Map.Entry.comparingByKey(Collator.getInstance()))
				.sorted(Map.Entry.comparingByValue())
				.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, Long::sum, LinkedHashMap::new))
				.forEach((slowo, ile) -> {
					System.out.printf("%-20s → %4d%n", slowo, ile);
				});
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}		
	}

}
