1 回答
TA贡献1995条经验 获得超2个赞
这是一个帮助您入门的简单示例。我已经采用了两种方法(问题列表和问题计数)。选择适合您需要的一个并删除另一个或同时使用两者:
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MainData {
public static void main(String[] args) throws IOException, FileNotFoundException {
String line = "";
String cvsSplitBy = ",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)";
try {
BufferedReader br = new BufferedReader(new FileReader("src/main/resources/rj.csv"));
Map<String, List<String>> questListByMinistry = new HashMap<>();
Map<String, Integer> questCountByMinistry = new HashMap<>();
//skip the first line (header)
br.readLine();
while ((line = br.readLine()) != null) {
String[] rj = line.split(cvsSplitBy);
if(!questCountByMinistry.containsKey(rj[2])){
//if the ministry doesn't exist as key yet put it to your map and put the value 1
questCountByMinistry.put(rj[2], 1);
}
else{
//else if it already exist get the current value and add +1
questCountByMinistry.put( rj[2], questCountByMinistry.get(rj[2])+1);
}
//-----------------------------------------------
if(!questListByMinistry.containsKey(rj[2])){
//if key doesen't exist put it to map and create a new list
questListByMinistry.put(rj[2], new ArrayList<>());
// and add the question to the list
questListByMinistry.get(rj[2]).add(rj[3]);
}
else{
//else if key already exists get the list associated to key and add the question
questListByMinistry.get(rj[2]).add(rj[3]);
}
}
System.out.println(questCountByMinistry);
System.out.println(questListByMinistry);
} catch (IOException e) {
e.printStackTrace();
}
}
}
如果您使用Java8或以上/如果您想熟悉Java8的功能
上面的代码可以重写为:
public static void main(String[] args) throws IOException, FileNotFoundException {
String line = "";
String cvsSplitBy = ",(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)";
try {
BufferedReader br = new BufferedReader(new FileReader("src/main/resources/rj.csv"));
Map<String, List<String>> questListByMinistry = new HashMap<>();
Map<String, Integer> questCountByMinistry = new HashMap<>();
//skip the first line
br.readLine();
while ((line = br.readLine()) != null) {
String[] rj = line.split(cvsSplitBy);
questListByMinistry.computeIfAbsent(rj[2], k -> new ArrayList<>()).add(rj[3]);
questCountByMinistry.compute(rj[2], (k,v) -> v==null? 1 : v+1);
}
System.out.println(questCountByMinistry);
System.out.println(questListByMinistry);
} catch (IOException e) {
e.printStackTrace();
}
}
添加回答
举报