1 回答

TA貢獻1995條經(jīng)驗 獲得超2個贊
這是一個幫助您入門的簡單示例。我已經(jīng)采用了兩種方法(問題列表和問題計數(shù))。選擇適合您需要的一個并刪除另一個或同時使用兩者:
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();
}
}
添加回答
舉報