java 字符串 包含 次数_利用JDK8的新特性计算某个目录下的文件中包含字符串的次数...

需求:计算某个字符串在某个文件夹中出现的次数。**这篇文章利用了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>> 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>) () -> {//创建N多个Callable实现类

Pair 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 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";

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值