利用JDK8的新特性计算某个目录下的文件中包含字符串的次数
2023-03-14 10:23:08 时间
需求:计算某个字符串在某个文件夹中出现的次数。**这篇文章利用了JDK1.8的新特性Stream流和Lambda表达式并结合了线程池的使用。**
package com.zkn.fullstacktraining.seventh;
import javafx.util.Pair;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.*;
import java.util.stream.Collectors;
/**
* Created by zkn on 2017/2/5.
*/
public class SearchStringByThreadPool {
public static void main(String[] args) {
try {
//创建5个固定线程的线程池
ExecutorService executorService = Executors.newFixedThreadPool(5);
List<Future<Pair<String, Integer>>> listFile =
//这里是取所传入目录的最多四层,如果不知道这个API的话需要递归去做。
Files.walk(Paths.get("D:\\CUST\\workspace\\JavaCore\\FullStackTraining\\src\\main\\java\\com\\zkn"), 4)
.filter(file -> !Files.isDirectory(file) && file.toString().endsWith("java"))//文件文件夹和不是java的文件
.map(file -> (Callable<Pair<String, Integer>>) () -> {//创建N多个Callable实现类
Pair<String, Integer> pair = null;//这里的键值对用pair比用Map更好一些
try {
Optional optional = Files.lines(file).map(str -> {
int count = 0;
int index = str.indexOf("main");
if (index >= 0) {
//这里需要循环计算,因为可能在某一行中会出现多次
do {
count++;
} while ((index = str.indexOf("main", index + 1)) > 0);
}
return count;
}).reduce(Integer::sum);//合并最终的计算结果
int count = optional.isPresent() ? (int) optional.get() :0;
pair = new Pair<>(file.toString(),count);
} catch (IOException e) {
e.printStackTrace();
}
return pair == null ? new Pair<>("", 0) : pair;
})
.map(file -> executorService.submit(file))//提交给线程池进行处理
.collect(Collectors.toList());
listFile.stream().map(file -> {
Pair<String, Integer> pair = null;
try {
pair = file.get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
return pair == null ? new Pair<>("", 0) : pair;
})
.filter(file -> file.getValue() > 0)//过滤掉不包含字符串的文件
.sorted((s1, s2) -> Integer.compare(s2.getValue(), s1.getValue()))//从大到小排序
.forEach(file -> System.out.println(String.format("%d次出现在%s文件中", file.getValue(), file.getKey())));
//关闭线程池
executorService.shutdown();
} catch (Exception e) {
e.printStackTrace();
}
}
public void test() {
String str = "mainmainmainmainmain";
}
}
相关文章
- 数据孤岛是业务效率的无声杀手
- 2023展望:新的一年将给大数据分析领域带来什么?
- 阿里云ADB基于Hudi构建Lakehouse的实践
- 大数据在医疗保健领域的使用案例
- 微软增加说明:KB5021751 更新扫描已经 / 即将过时 Office 过程中不会触碰用户隐私
- 2022 Gartner全球云数据库管理系统魔力象限发布 腾讯云数据库入选
- 场景化、重实操,分享一个实时数仓实践案例
- Arctic的湖仓一体践行之路
- 分布式计算MapReduce究竟是怎么一回事?
- 淘系数据模型治理优秀实践
- 大数据分析对医疗保健的影响
- 当我们说大数据Hadoop,究竟在说什么?
- 2022年及以后大数据的五个发展趋势
- 网易严选离线数仓治理实践
- 2023 年数据治理趋势
- 一份“靠谱”的年度经营计划,你学会了吗?
- 漫谈对大数据的思考
- 测试一下,读懂数据的能力,你有吗?
- 用艺术的眼光探索数据之美
- 聊聊数据分析成果如何落地