[代码] 根据homework\homework.html文件,返回<学生ID,交作业顺序>的Map

来源:互联网 发布:视频动态截图软件 编辑:程序博客网 时间:2024/05/16 05:12
/**
* 根据homework\homework.html文件,返回<学生ID,交作业顺序>的Map
* @param file  homework.html文件
* @return 一个Map,存储<学生ID,交作业顺序>
*/
public static HashMap<String,Integer> getSubmitOrder(File file) {
HashMap<String,Integer> resultmap = new HashMap<String,Integer>();
ArrayList<String> studentIDlist = new ArrayList<String>();
ArrayList<String> submitTimelist = new ArrayList<String>();

if(!file.exists()){
throw new IllegalArgumentException("文件不存在!");
}
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(file));
String str = null;
while((str=br.readLine()) != null){
if(str.startsWith("\t<tr><td>")){
//处理: <tr><td>1310817016</td><td>庞威</td><td>2015年11月12日 22:28:59</td><td>...
//提取学号
Pattern pattern1 = Pattern.compile(">\\d{10}<");
Matcher matcher1  = pattern1.matcher(str);
while(matcher1.find()){
studentIDlist.add(matcher1.group().substring(1, 11));
}
//提取交作业的日期信息
Pattern pattern2 = Pattern.compile("\\d{4}年\\d{1,2}月\\d{1,2}日 \\d{2}:\\d{2}:\\d{2}");
Matcher matcher2  = pattern2.matcher(str);
while(matcher2.find()){
submitTimelist.add(matcher2.group());
}
//使用IdentityHashMap,可以处理交作业日期一致的学生情况,IdentityHashMap的Key允许重复
IdentityHashMap<String, String> tm = new IdentityHashMap<String, String>();
for(int i = 0;i<submitTimelist.size();i++){
tm.put(submitTimelist.get(i), studentIDlist.get(i));
}
Set<String> submitTimeset = tm.keySet();
ArrayList<String> templist = new ArrayList<String>(submitTimeset);
Collections.sort(templist);//对交作业日期进行排序
int order = 1;
for(String s:templist){
resultmap.put(tm.get(s),order++);//将对应的学号加入结果List中
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(br!=null){
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return resultmap;
}
0 0
原创粉丝点击