词频计数 Java 8

Word frequency count Java 8(词频计数 Java 8)

本文介绍了词频计数 Java 8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java 8中如何统计List的词频?

How to count the frequency of words of List in Java 8?

List <String> wordsList = Lists.newArrayList("hello", "bye", "ciao", "bye", "ciao");

结果必须是:

{ciao=2, hello=1, bye=2}

推荐答案

我想分享我找到的解决方案,因为一开始我希望使用 map-and-reduce 方法,但它有点不同.

I want to share the solution I found because at first I expected to use map-and-reduce methods, but it was a bit different.

Map<String, Long> collect = 
        wordsList.stream().collect(groupingBy(Function.identity(), counting()));

或者对于整数值:

Map<String, Integer> collect = 
        wordsList.stream().collect(groupingBy(Function.identity(), summingInt(e -> 1)));

编辑

我添加了如何按值对地图进行排序:

I add how to sort the map by value:

LinkedHashMap<String, Long> countByWordSorted = collect.entrySet()
            .stream()
            .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
            .collect(Collectors.toMap(
                    Map.Entry::getKey,
                    Map.Entry::getValue,
                    (v1, v2) -> {
                        throw new IllegalStateException();
                    },
                    LinkedHashMap::new
            ));

这篇关于词频计数 Java 8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:词频计数 Java 8

基础教程推荐