猿问

解析csv文件并在java集合hashmap中显示特定信息

我正在练习java,这对我来说是新的,最近学习了java中的集合。我想解析 csv 文件并将其存储在哈希映射上。另外,我不想使用任何解析器。


我的CSV文件:-


id,date,ministry,questions

2011,15.02.2014,HEALTH,What was the outcome

20757,24.02.2015,"DEFENCE , FINANCE" ,"Your budget this year .."

20113,17.03.2013,HEALTH, Hospitals build

所以,我有几个问题:-


我想"DEFENCE , FINANCE"在同一列中。我将如何使用正则表达式删除“,”,以便分隔符不会设置新列

我想显示每个部委提出的问题数量。例如:-健康总共有 2 个问题等。

亦无重复。

我正在通过 I/O 文件读取器进行解析。


我的代码:-

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"));

        HashMap<String,String> rjFile = new HashMap<String, String>();

        System.out.println("running"+rjFile);



        while ((line = br.readLine()) != null) {


            String[] rj = line.split(cvsSplitBy);


            System.out.println(br);

        }

    } catch (IOException e) {

        e.printStackTrace();

    }

    }

}

PS:-我只想使用与地图相关的集合。


MYYA
浏览 107回答 1
1回答

拉风的咖菲猫

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

相关分类

Java
我要回答