package Wordcount;
import javax.print.DocFlavor;
import javax.swing.plaf.synth.SynthLookAndFeel;
import java.awt.*;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.*;
import java.sql.SQLOutput;
import java.util.*;
/*
如何读取一个目录下多个文件中单词的个数,写入到文件中
1.读取目录(目录下都是文件) listFiles
2.读取文件中内容 创建数组保存(保存的是每个单词)
3.循环之后进行储存(MapString,Interger) 1
4.分组(以单词个数进行分组) 首先判断分几组
5.聚合 判断Map中不同单词存储在不组之中(例如:java都存储在list之中,而hello都存储在list)
6.计算每个单词出现的次数(循环每个组中的元素 进行累加)(例如:java,15 hello,20)
7.写入数据到文件
*/
public class text {
//创建集合保存数据要内加载时加载
//读取每一行的数据
private static ArrayList<String> list;
private static ArrayList<HashMap<String, Integer>> ListMap;
//用来去重得到应该分几组
private static HashSet<HashMap<String,Integer>> set;
//以单词进行分组
private static ArrayList<String> group;
//创建机集合保存四组的数据
private static HashMap<String,Integer> map;
//定义HashMap保存汇总的结果
private static ArrayList<ArrayList<HashMap<String,Integer>>> groupEnd;
public static void main(String[] args) throws IOException{
readFile();
split(list);
/*for(HashMap<String,Integer> map:ListMap) {
Set<Map.Entry<String,Integer>> entries=map.entrySet();
for(Map.Entry<String,Integer> m:entries) {
System.out.println(m.getKey()+","+m.getValue());
}
}*/
groupSum(ListMap);
groupList(group,ListMap);
/*for(ArrayList<HashMap<String,Integer>> list:groupEnd) {
for(HashMap<String,Integer> map:list) {
Set<Map.Entry<String,Integer>> entries=map.entrySet();
for(Map.Entry<String,Integer> m:entries) {
System.out.println(m.getKey()+','+m.getValue());
}
}
}*/
groupCount(groupEnd);
Set<Map.Entry<String,Integer>> entries=map.entrySet();
for(Map.Entry<String,Integer> m:entries) {
System.out.println(m.getKey()+','+m.getValue());
}
}
//1.读取数据
public static ArrayList<String> readFile() throws IOException {
list=new ArrayList<String>();
//读取文件对象
File file=new File("C:\\Users\\riying\\Desktop\\课程文件夹\\java\\java培训\\5.14");
//获取当前目录下所有文件的对象
File[] files=file.listFiles();
//首先获取文件的路径
for(File f:files) {
System.out.println(f.getAbsolutePath());
//创建流读取数据 创建字符缓冲流 室友readline()u一行一行读取数据
BufferedReader br=new BufferedReader(new FileReader(f.getAbsolutePath()));
//创建string类型的变量用来判断是否读取结束
String str;
while((str=br.readLine())!=null) {
//读取的数据要保存,存在集合之中ArrayList
list.add(str);
}
br.close();
}
return list;
}
//2.数据切分,参数为之前读取的书用来接收数据的Arraylist集合
public static ArrayList<HashMap<String,Integer>> split(ArrayList<String> list) {
ListMap=new ArrayList<HashMap<String,Integer>>();
for(String s:list) {
//手动进行切分
String[] split=s.split(",");
for(String str:split) {
//3.储存为Map格式
HashMap<String,Integer> hashMap=new HashMap<>();
hashMap.put(str,1);
//为了下面方便计算,每个hashmap对象之中只有一条数据
ListMap.add(hashMap);
}
}
return ListMap;
}
//3、分组,首先确定分几组(使用set集合,Set不能存储相同数据)
public static ArrayList<String> groupSum(ArrayList<HashMap<String,Integer>> listMap) {
set=new HashSet<HashMap<String, Integer>>();
group=new ArrayList<String>();
for(HashMap<String,Integer> hashMap:listMap) {
set.add(hashMap);
}
for(HashMap<String,Integer> map:set) {
Set<Map.Entry<String,Integer>> entries = map.entrySet();
//数据储存在HashSet集合重自动去重,得到了分几组的数据。
for(Map.Entry<String,Integer> m:entries) {
//按什么分组,存储到了ArrayList<String> group中
group.add(m.getKey());
}
}
return group;
}
//第四步开始分组+聚合,以group中的数据来分组
public static ArrayList<ArrayList<HashMap<String, Integer>>> groupList(ArrayList<String> group,ArrayList<HashMap<String,Integer>> listMap) {
groupEnd=new ArrayList<ArrayList<HashMap<String, Integer>>>();
//开始分组
for(String str:group) {
ArrayList<HashMap<String,Integer>> list=new ArrayList<>();
//循环数据
for(HashMap<String,Integer> map:listMap) {
Set<Map.Entry<String,Integer>> entries=map.entrySet();
for(Map.Entry<String,Integer> m:entries) {
//判断开始分组,判断数据是否和分组相同
if(str.equals(m.getKey())) {
//酒吧数据添加到相同的组之中
list.add(map);
}
}
}
groupEnd.add(list);
}
return groupEnd;
}
//5.汇总统计每个组之中的的单词个数
public static HashMap<String,Integer> groupCount(ArrayList<ArrayList<HashMap<String,Integer>>> groupEnd) {
map = new HashMap<String, Integer>();
for(ArrayList<HashMap<String,Integer>> list:groupEnd) {
//因为输出的结果为hello,6这种数据
int count=0;
String str="";
for(HashMap<String,Integer> map:list) {
Set<Map.Entry<String,Integer>> entries=map.entrySet();
for(Map.Entry<String,Integer> m:entries) {
str=m.getKey();
count+=1;
}
}
//每一组循环之后再添加数据
map.put(str,count);
}
return map;
}
}
这可能是你看过WordCount最详细的代码!!!
猜你喜欢
转载自blog.csdn.net/qq_45750230/article/details/106178395
今日推荐
周排行