private static Map<String,Integer> toMapFunction(Collection< ? extends String> collection){
return collection.stream().map(String::toLowerCase)
.filter(str -> !str.trim().isEmpty())
.collect(Collectors.toMap(Function.identity(), value -> 1, (oldValue, newValue) -> oldValue + newValue, TreeMap::new));
}
public static void main(String[] args) {
List<String> stringList = Arrays.asList("joy", "joy", "lobo", "lobo", "lobo", "joy", "joy", "joy", "david", "hibbs");
System.out.println(toMapFunction(stringList));
}
и это будет вывод программы:
{david=1, hibbs=1, joy=5, lobo=3}