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和鼠标左键垂直上下拖动可以跨行在指定位置输入

浙公网安备 33010602011771号