如何计算Java 8中List的单词频率?
List <String> wordsList = Lists.newArrayList("hello", "bye", "ciao", "bye", "ciao");
Run Code Online (Sandbox Code Playgroud)
结果必须是:
{ciao=2, hello=1, bye=2}
Run Code Online (Sandbox Code Playgroud) 如果要对流中的整数值求和,有两种主要方法:
ToIntFunction<...> mapFunc = ...
int sum = stream().collect(Collectors.summingInt(mapFunc))
int sum = stream().mapToInt(mapFunc).sum()
Run Code Online (Sandbox Code Playgroud)
第一个涉及装箱返回的整数并将其拆箱,但第二个步骤涉及额外的步骤.
哪个更有效/更清晰?