假如我的一个文档大小有20M内容为 1 5 6 9 11
2 4 8 10 11
1 7 8 9 14
..........
然后我想读取出来,并和已知的4 5 6 7 8 9对比 判断出里面有一个相同数字的有几组 有两个相同数字的有几组,并写会领一个文本文档里面该怎么做 求大神代码
2 4 8 10 11
1 7 8 9 14
..........
然后我想读取出来,并和已知的4 5 6 7 8 9对比 判断出里面有一个相同数字的有几组 有两个相同数字的有几组,并写会领一个文本文档里面该怎么做 求大神代码
/*
* 测试文件data.txt内容:
12 4 56 63 25 79 21 45
12 41 56 63 6 79 21 45
12 42 56 63 25 79 21 45
12 43 56 8 25 79 21 45
12 44 56 63 25 79 9 45
12 4 56 63 5 79 21 45
12 41 56 63 6 79 21 45
12 42 56 63 8 79 21 45
12 43 56 8 25 79 21 45
12 44 56 63 25 79 9 45
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.FileWriter;import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.Path;
import static java.nio.file.StandardOpenOption.*;import java.util.stream.Stream;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.regex.Matcher;
public class Test{
public static void main(String[] args){
String[] contents = {"4","5","6","7","8","9"};
String file = "data.txt";
Process process = new Process(contents);
Task task1 = new Task(process,1,file);
Task task2 = new Task(process,2,file);
Thread thread1 = new Thread(task1);
Thread thread2 = new Thread(task2);
thread1.start();
thread2.start();
}
}class Task implements Runnable{
public Task(Process process,int findCount,String file){
this.process = process;
this.findCount = findCount;
this.file = file;
} @Override
public void run(){
process.process(file,findCount);
} private Process process;
private int findCount;
private String file;
}class Process{ public Process(String[] contents){
StringBuffer sb = new StringBuffer("(?<=^|\\D)(");
boolean condition = true;
for(String content : contents){
if(condition){
sb.append(content);
condition = false;
continue;
}
sb.append("|" + content);
}
sb.append(")(?=$|\\D)");
regex = sb.toString();
}
public void process(String file,int findCount){
Path path = Paths.get(file);
assert Files.isRegularFile(path);
MyPredicate predicate = new MyPredicate(regex,findCount);
String saveFileName = file.replaceAll("\\.","(出现" + findCount + "次).");
try(BufferedReader reader = new BufferedReader(
new InputStreamReader(
Files.newInputStream(path,READ)));
Stream<String> result = reader.lines();
Stream<String> result1 = result.filter(predicate);
FileWriter writer = new FileWriter(saveFileName);
){
Object[] contents = result1.toArray();
for(Object content : contents){
writer.write(content + System.lineSeparator());
}
}catch(IOException e){
e.printStackTrace();
System.exit(1);
}
} private final String regex;
}class MyPredicate implements Predicate<String>{ public MyPredicate(String regex,int findCount){
pattern = Pattern.compile(regex);
this.findCount = findCount;
} @Override
public boolean test(String content){
Matcher matcher = pattern.matcher(content);
int time = 0;
while(matcher.find()){
time ++;
}
return time == findCount;
} private Pattern pattern;
private int findCount;
}
public class Main{ public static void main(String[] args) throws Exception{
List<String> sourceList = Arrays.asList("4 5 6 7 8 9".split(" ")); // 统计分组
Map<Integer, Integer> compareResult = new HashedMap(); // 这里换成file 一行一行读入
for (String line : new String[]{"1 5 6 9 11", "2 4 8 10 11", "1 7 8 9 14"}) {
Integer compareCount = intersection(sourceList, Arrays.asList(line.split(" ")));
if(compareCount > 0){
Integer totalValue = 0;
if(compareResult.containsKey(compareCount)){
totalValue = compareResult.get(compareCount);
}
compareResult.put(compareCount, ++totalValue);
}
} // 排序
List<Integer> groupKeys = new ArrayList<>(compareResult.size());
groupKeys.addAll(compareResult.keySet()); Collections.sort(groupKeys); // 输出
for(Integer groupKey : groupKeys){
System.out.println(String.format("有%s个相同数字的有%s组", groupKey, compareResult.get(groupKey)));
}
} private static Integer intersection(List<String> source, List<String> target) {
int count = 0; for (String targetItem: target) {
if(source.contains(targetItem)) {
count++;
}
} return count;
}
}输出:
有2个相同数字的有1组
有3个相同数字的有2组
欢迎光临我的博客
http://happyshome.cn