Код: Выделить всё
String[] input = new String[] {
"This is a sample string",
" string ", // additional spaces here cause issues while splitting
"Another sample string",
"This is not a sample string"
};
Код: Выделить всё
{a=2, not=1, string=4, This=2, is=2, sample=3, Another=1}
Код: Выделить всё
// 1. Convert String[] into a single " " delimited String
String joined = String.join(" ", input);
// 2. Split on " " and then calculate count using Collectors.groupingBy
Map output = Arrays.stream(joined.split(" "))
.filter(s -> !s.equals("")) // To Deal with Empty Strings
.collect(Collectors.groupingBy(Function.identity(),Collectors.counting()));
System.out.println(output);
Спасибо.
Подробнее здесь: https://stackoverflow.com/questions/791 ... ing-java-8