简体   繁体   English

Java - 如何将 CSV 文件读入 Map <string, map<string, string> ></string,>

[英]Java - How to read a CSV file into a Map<String, Map<String, String>>

Input (CSV File)输入(CSV 文件)

Header1,Header2,Header3

Data1,Data2,Data3

Data4,Data5,Data6

Data7,,

,,Data8

I am trying to parse through a CSV file similar to the above structure and store the data into a Map<String (Row Number), Map<String (Column Header), String (Column Value)>>.我正在尝试解析一个类似于上述结构的 CSV 文件,并将数据存储到 Map<String (Row Number), Map<String (Column Header), String (Column Value)>> 中。

Assuming your file is well formed and each row has the same column count as your header, something like below migght be a starting point:假设您的文件格式正确,并且每一行的列数与您的 header 相同,下面的内容可能是一个起点:

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

public final class Example {

    public static void main(String[] args) throws IOException {

        //create a map    
        Map<String, Map<String, String>> map = new LinkedHashMap<>();

        Path filePath = Paths.get("path to your/test.csv");
        Charset charset = StandardCharsets.UTF_8;

        //read the file
        List<String> fileContent = Files.readAllLines(filePath, charset);

        // get the header
        String[] header = fileContent.get(0).split(",");

        AtomicInteger lineNumber = new AtomicInteger(1);

        //fill the map
        fileContent.stream()
                   .skip(1)
                   .forEach(line -> map.put(
                           String.valueOf(lineNumber.getAndIncrement()),
                           getRowData(header, line.split(","))));

        System.out.println(map);
    }

    // helper method to make the filling of the map in the stream readable
    private static Map<String, String> getRowData(final String[] header, final String[] line) {
        return IntStream.range(0, header.length)
                        .mapToObj(i -> Map.entry(header[i], line[i]))
                        .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 读取文件并映射到地图 <String, List<String> &gt; - Read a file and map to a Map<String, List<String>> 将 CSV 文件处理为 Map<string,string[]> 使用 Java 8</string,string[]> - Processing a CSV File to Map<String,String[]> using Java 8 如何转换地图 <String, List<String> &gt;到地图 <String, String> 在java 8中 - How to Convert a Map<String, List<String>> to Map<String, String> in java 8 如何转换地图 <String, Set<String> &gt;到地图 <String, String[]> 在Java中 - How to convert Map<String, Set<String>> to Map<String, String[]> in Java 如何将 CSV 文件转换为列表<Map<String,String> &gt; - How to convert a CSV file to List<Map<String,String>> Java 8:如何将String转换为Map <String,String> ? - Java 8: How to convert String to Map<String,String>? 如何将 Excel 文件读入列表<Map<String, String> &gt; - How can I read Excel file into List<Map<String, String>> 如何将字符串解析为map,使用Java从文件位置读取字符串 - How parse string into map,the string read from a file location using java Java地图 <String, MyObject> 到地图 <String, String> - Java Map<String, MyObject> to Map<String, String> 如何将Map <String,string>添加到java中的Map <String,List <Object >>中 - How to add Map<String, string> into Map<String, List<Object>> in java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM