import org.eclipse.collections.api.bag.MutableBag; import org.eclipse.collections.api.list.MutableList; import org.eclipse.collections.impl.factory.Bags; import org.eclipse.collections.impl.factory.Lists; import org.eclipse.collections.impl.utility.StringIterate; import java.nio.file.Files; import java.nio.file.Paths; import java.util.stream.Stream; public class WordCounter { public static void main(String[] args) throws Exception { String filePath = "path/to/text/file.txt"; Stream<String> lines = Files.lines(Paths.get(filePath)); Stream<String> words = lines.flatMap(line -> StringIterate.tokensAsList(line).stream()); MutableBag<String> wordBag = Bags.mutable.empty(); words.forEach(word -> wordBag.add(word)); wordBag.forEachWithOccurrences((word, count) -> } } <dependency> <groupId>org.eclipse.collections</groupId> <artifactId>eclipse-collections</artifactId> <version>10.4.0</version> </dependency>


上一篇:
下一篇:
切换中文