本地文件夹中存在以下几个文件:
StudentClassMaxGrade.txt、StudentGrade.txt、StudentMessage.txt
其中,StudentGrade.txt存储格式为:
1500100001,1000001,98
1500100001,1000002,5
1500100001,1000003,137
1500100001,1000004,29
1500100001,1000005,85
1500100001,1000006,52
1500100002,1000001,139
1500100002,1000002,102
1500100002,1000003,44
1500100002,1000004,18
1500100002,1000005,46
1500100002,1000006,91…
StudentMessage.txt存储格式为:
1500100001,施笑槐,22,女,文科六班
1500100002,吕金鹏,24,男,文科六班…
代码段如下:
public class StudentClassMaxGrade {
public static void main(String[] args) {
result();
}
//求每个学生的总分
public static ArrayList<String> sumGrade() {
ArrayList<String> list = new ArrayList<>();//用于存储每个学生的总分
//读文件
BufferedReader messagefile = null;
try {
messagefile = new BufferedReader(new FileReader("D:\\DATA\\IdeaProjects\\BigData1\\StudentData\\StudentMessage.txt"));
//循环读取文件中学生的信息和成绩
String sMessage;
String sGrade;
while ((sMessage = messagefile.readLine()) != null) {
int sum = 0;
BufferedReader gradefile = null;
try {
gradefile = new BufferedReader(new FileReader("D:\\DATA\\IdeaProjects\\BigData1\\StudentData\\StudentGrade.txt"));
while ((sGrade = gradefile.readLine()) != null) {
//匹配学生信息和学生成绩的共同元素:学号
if (sMessage.split(",")[0].equals(sGrade.split(",")[0])) {
//将同一学号的学生的成绩累加
sum += Integer.valueOf(sGrade.split(",")[2]);
}
}
} catch (IOException e) {
e.printStackTrace();
} catch (NumberFormatException e) {
e.printStackTrace();
} finally {
if (gradefile != null)
try {
gradefile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//创建新的StringBuilder存储学生的信息和成绩
StringBuilder sumGrade = new StringBuilder();
sumGrade.append(sMessage).append(",").append(String.valueOf(sum));
list.add(sumGrade.toString());
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (messagefile != null)
try {
messagefile.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//遍历存储学生信息和成绩的列表
/*for (String s:list) {
System.out.println(s);
}*/
return list;
}
//求每个班级的最高分的学生
public static HashMap<String, String> classTopGrade() {
HashMap<String, String> map = new HashMap<>();//存储最终的结果每个班级的最高分
//循环遍历存储学生成绩的列表,抽取出班级信息作为存HashMap的k值
for (String s : sumGrade()) {
if (!(map.containsKey(s.split(",")[4]))) {
map.put(s.split(",")[4], "");
}
}
//迭代HashMap和遍历学生成绩的列表,匹配找出最高分
Set<Map.Entry<String, String>> entries = map.entrySet();
for (Map.Entry<String, String> entry : entries) {
int max = 0;
//遍历学生成绩的列表,找出最高分
for (String s : sumGrade()) {
if (entry.getKey().equals(s.split(",")[4])) {
if (max < Integer.valueOf(s.split(",")[5])) {
max = Integer.valueOf(s.split(",")[5]);
}
}
}
//遍历学生成绩的列表,找出最高分对应的学生信息
for (String s : sumGrade()) {
if (max == Integer.valueOf(s.split(",")[5])) {
map.put(entry.getKey(), s.split(",")[1] + ",成绩为:" + s.split(",")[5]);
}
}
}
//遍历最终结果列表并展示
for (Map.Entry<String, String> entry : entries) {
System.out.println(entry.getKey() + "最高分是:" + entry.getValue());
}
return map;
}
//最终的结果存在文件里
public static void result() {
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new FileWriter("D:\\DATA\\IdeaProjects\\BigData1\\StudentData\\StudentClassMaxGrade.txt"));
for (Map.Entry<String, String> entry : classTopGrade().entrySet()) {
bw.write(entry.getKey() + "最高分是:" + entry.getValue());
bw.write("\r\n");
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (bw != null)
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
主要几个值得新手注意的点:读写文件的异常处理、汇总聚合利用文件的共同元素以及hashmap中K值不重复的做法提取唯一信息班级,然后进行遍历处理以及String类的常见用法。
阿培(本博主)能力有限,
如有更好的办法后续会改进!
欢迎大家互相讨论!加油!