java8--项目--词频统计
package stream;
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;
import java.util.regex.Pattern;
import java.util.stream.Stream;
public class CountLongWords0320250103 {
public static void main(String[] args) throws IOException {
long count;
count = Listcount(); //for each遍历数组
System.out.println(count);
count = ListToStreamcount();//转换成流后用filter方法lambda表达式传入条件过滤出
System.out.println(count);
count = ListToParallelStreamcount(); //上一个的多线程版
System.out.println(count);
count = Streamcount(); //以非字母分隔符分割后立刻转换成流,用filter过滤
System.out.println(count);
}
public static long Streamcount(){
String contents = null;
try {
contents = new String(
Files.readAllBytes(
Paths.get("C:\\Users\\Administrator\\IdeaProjects\\untitled2\\src\\gutenberg\\alice30.txt")),
StandardCharsets.UTF_8
);
} catch (IOException e) {
e.printStackTrace();
}
Stream<String> words = Pattern.compile("\\PL+").splitAsStream(contents);
long count = 0;
count = words.filter(w->w.length() > 12).count();
return count;
}
public static long ListToParallelStreamcount(){
String contents = null;
try {
contents = new String(
Files.readAllBytes(
Paths.get("C:\\Users\\Administrator\\IdeaProjects\\untitled2\\src\\gutenberg\\alice30.txt")),
StandardCharsets.UTF_8
);
} catch (IOException e) {
e.printStackTrace();
}
List<String> words = Arrays.asList(contents.split("\\PL+"));
long count = 0;
count = words.parallelStream().filter(w -> w.length() > 12).count();
return count;
}
public static long ListToStreamcount(){
String contents = null;
try {
contents = new String(
Files.readAllBytes(
Paths.get("C:\\Users\\Administrator\\IdeaProjects\\untitled2\\src\\gutenberg\\alice30.txt")),
StandardCharsets.UTF_8
);
} catch (IOException e) {
e.printStackTrace();
}
List<String> words = Arrays.asList(contents.split("\\PL+"));
long count = 0;
count = words.stream().filter(w -> w.length() > 12).count();
return count;
}
public static long Listcount(){
String contents = null;
try {
contents = new String(
Files.readAllBytes(
Paths.get("C:\\Users\\Administrator\\IdeaProjects\\untitled2\\src\\gutenberg\\alice30.txt")),
StandardCharsets.UTF_8
);
} catch (IOException e) {
e.printStackTrace();
}
List<String> words = Arrays.asList(contents.split("\\PL+"));
long count = 0;
for (String w:
words) {
if (w.length()>12) count++;
}
return count;
}
}
ps:
1.java虚拟机在项目根目录下启动
2.路径还可以用.\\src\\gutenberg\\alice30.txt
或./src/gutenberg/alice30.txt
,也可以反斜杠和斜杠混用,比如./src/gutenberg\\alice30.txt
,路径里的反斜杠需要用反斜杠转义,这里已经转义了
20250104第一次追加pps:
补充方法--统计行:
public static long LinesStreamcount(){
long count = 0;
Path path = Paths.get("./src/gutenberg/alice30.txt");
try {
try(Stream<String> lines = Files.lines(path)){
count = lines.filter(w -> w.length() > 12).count();
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println(count);
return count;
}
2.在我的idea中按住Alt和鼠标左键垂直上下拖动可以跨行在指定位置输入
标签:
Java
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律