第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

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

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

MYYA 2023-12-13 16:32:15
我正在練習(xí)java,這對我來說是新的,最近學(xué)習(xí)了java中的集合。我想解析 csv 文件并將其存儲在哈希映射上。另外,我不想使用任何解析器。我的CSV文件:-id,date,ministry,questions2011,15.02.2014,HEALTH,What was the outcome20757,24.02.2015,"DEFENCE , FINANCE" ,"Your budget this year .."20113,17.03.2013,HEALTH, Hospitals build所以,我有幾個問題:-我想"DEFENCE , FINANCE"在同一列中。我將如何使用正則表達式刪除“,”,以便分隔符不會設(shè)置新列我想顯示每個部委提出的問題數(shù)量。例如:-健康總共有 2 個問題等。亦無重復(fù)。我正在通過 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:-我只想使用與地圖相關(guān)的集合。
查看完整描述

1 回答

?
拉風(fēng)的咖菲貓

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

    }

}


查看完整回答
反對 回復(fù) 2023-12-13
  • 1 回答
  • 0 關(guān)注
  • 171 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號