一切尽在代码中…
package com.example.demo;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
public class CountLongWords {
public static void main(String[] args) throws IOException {
//读取该路径文件内容
String contents = new String(Files.readAllBytes(
Paths.get("D:\gutenberg\Test.txt")), StandardCharsets.UTF_8);
//对获取到的文件内容进行切割,装进集合中。
List words = Arrays.asList(contents.split("\PL+"));
long count = 0;
for (String word : words) {
if (word.length() > 10) {
count++;
}
}
System.out.println(count);
count = words.stream().filter(w -> w.length() > 10).count();
System.out.println(count);
count = words.parallelStream().filter(w -> w.length() > 10).count();
System.out.println(count);
count = words.stream().limit(5).filter(w -> w.length() > 10).count();
System.out.println(count);
}
}
1.使用正则表达式



