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

标签 java csv hashmap

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

我的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:- 我只想使用与 map 相关的集合。

最佳答案

这是一个帮助您入门的简单示例。我已经采用了两种方法(问题列表和问题计数)。选择适合您需要的一个并删除另一个或同时使用两者:

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();
    }
}

关于java - 解析csv文件并在java集合hashmap中显示特定信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58539298/

相关文章:

java - 表示层(JSF)应如何与服务层(Spring)互操作?

string - 在 Lua 中处理 TSV 文件

php - CSV 导出 : Multiple Database Rows to Single Row

java - 通过电子邮件搜索打印

java - Apache 点燃: Object cannot be found in Hashmap after deserialization

Java 对象数组在初始化期间崩溃

java - 使用 Java 在 Selenium WebDriver 中使用 PageObjects、Page Factory 和 WebDriverWait

java - 简单的Java方法?

ruby - 如何在许多匹配项中使用 ruby​​ gsub Regexp?

java - Java中HashMap的解析