Use jdk8 stream to count words

  Some students asked how to use stream to count the number of words. This is a good example, and it is typical, to be added here.

  The following example implements the function of reading (English) text from a text file and counting the number of words.

  package com.imooc;

  import java.io.BufferedReader;

  import java.io.FileReader;

  import java.io.IOException;

  /**

  * Use stream to count the number of words in the article

  *

  * @author Xiaofengqing

  *

  */

  public class StreamWordDemo {

  public static void main(String[] args) throws IOException {

  // Use try-resource to close the resource

  try (BufferedReader reader = new BufferedReader(

  new FileReader(webflux.txt))) {

  long wordCount = reader.lines()

  // trim before and after spaces (using method references)

  .map(String::trim)

  // filter out empty strings

  .filter(s - !s.isEmpty())

  // Convert the space separated into an array of words

  .map(s - s.split( ))

  // get the length of the array

  .mapToInt(array - array.length)

  // Parallel (all stateless operations)

  .parallel()

  // sum

  .sum();

  System.out.println(word count: + wordCount);

  }

  }

  }

  The knowledge points involved are mainly the basic applications of lambda expressions and streams. As you can see, programming code using streams is very clear and simple, and very readable.

  The following gets the number of occurrences of each word

  // Use try-resource to close the resource

  try (BufferedReader reader = new BufferedReader(

  new FileReader(webflux.txt))) {

  MapString, Long counts = reader.lines()

  // trim before and after spaces (using method references)

  .map(String::trim)

  // filter out empty strings

  .filter(s - !s.isEmpty())

  // 把空格隔开的转为数组

  .map(s - s.split( ))

  // 数组转成流

  .map(array - Stream.of(array))

  // 拉平

  .flatMap(stream - stream)

  // 分组

  .collect(Collectors.groupingBy(s - s, Collectors.counting()));

  System.out.println(单词出现次数: + counts);

  // 统计信息

  LongSummaryStatistics summaryStatistics = counts.entrySet().stream()

  // 得到次数

  .mapToLong(entry - entry.getValue())

  // 统计

  .summaryStatistics();

  System.out.println(统计信息: + summaryStatistics);

  }

  输出的统计信息为:

  统计信息:LongSummaryStatistics{count=170, sum=271, min=1, average=1.594118, max=14}

  可以看出,一共有271个单词,不同的词有170个,出现最多的14次,最少1次,平均1.594118次。

  这就是stream的编程风格,其中lambda表达式是函数式编程的基础,是后面的stream,reactor的前置基础知识。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325929652&siteId=291194637
Recommended