如何将地图列表转换为CSV

时间:2017-06-08 07:44:23

标签: java list csv dictionary

我需要将地图列表转换为CSV对象,如下所示

List<Map<String,Object>> maps = new ArrayList<Map<String,Object>>();

maps对象包含以下格式的值

地图1:

(header 1, value1)

(header 2, value2)

(header 3, value3)

(header 4, value4)

地图2:

(header 1, value5)

(header 2, value6)

(header 3, value7)

(header 4, value8)

我正在寻找下面的

的CSV
header1, header2,header3, header4

value1, value2,value3,value4

value5,value6,value7,value8

我试图读取地图(键,值)并写入CSV文件,但是它写入格式

标题1,value1

header2,value2

标题3,value3

header4,value4

header1,value5

下面是我尝试过的代码片段

(Map<String, Object> map : maps) {

 for (Map.Entry<String, Object> entry : map.entrySet()) {

w.append(entry.getKey()).append(",").append(entry.getValue()‌​.toString()).append(‌​"\n"); } } 

3 个答案:

答案 0 :(得分:8)

以下代码可以帮助您。它还没有为你的目的做好准备。您需要更改它,因此它会打印到文件而不是返回String

private static String toCSV(List<Map<String, Object>> list) {
    List<String> headers = list.stream().flatMap(map -> map.keySet().stream()).distinct().collect(toList());
    final StringBuffer sb = new StringBuffer();
    for (int i = 0; i < headers.size(); i++) {
        sb.append(headers.get(i));
        sb.append(i == headers.size()-1 ? "\n" : ",");
    }
    for (Map<String, Object> map : list) {
        for (int i = 0; i < headers.size(); i++) {
            sb.append(map.get(headers.get(i)));
            sb.append(i == headers.size()-1 ? "\n" : ",");
        }
    }
    return sb.toString();
}

对于以下输入,它给出了以下输出:

public static void main(String[] args) {
    List<Map<String, Object>> list = new ArrayList<>();
    Map<String, Object> map1 = new HashMap<>();
    map1.put("header1", "value1");
    map1.put("header2", "value2");
    map1.put("header3", "value3");
    map1.put("header4", "value4");
    Map<String, Object> map2 = new HashMap<>();
    map2.put("header1", "value5");
    map2.put("header2", "value6");
    map2.put("header3", "value7");
    map2.put("header4", "value8");
    list.add(map1);
    list.add(map2);
    System.out.println(toCSV(list));
}

enter image description here

这种形式是完全可逆的,因此您可以编写自己的方法从此类CSV读取到List<Map<String, String>

另一个例子,当其中一个地图中没有钥匙时:

enter image description here

答案 1 :(得分:0)

考虑以下列表

[
{
"header1": "value1",
"header2": "value2",
"header3": "value3",
"header4": "value4"
},
{
"header1": "value1",
"header2": "value2",
"header3": "value3",
"header4": "value4"
},
{
"header1": "value1",
"header2": "value2",
"header3": "value3",
"header4": "value4"
}
]

为了将其隐藏到CSV文件中,我添加了一个简单的方法...。

public String createCSV(List<LinkedHashMap<String, String>> list) throws IOException{
    List<String> headers = list.stream().flatMap(map -> map.keySet().stream()).distinct().collect(Collectors.toList());

    String path= "dataCSV.csv";

    try(FileWriter writer= new FileWriter(path, true);){
           for (String string : headers) {
                 writer.write(string);
                 writer.write(",");
           }
           writer.write("\r\n");

           for (LinkedHashMap<String, String> lmap : list) {
                 for (Entry<String, String> string2 : lmap.entrySet()) {
                        writer.write(string2.getValue());
                        writer.write(",");
                 }
                 writer.write("\r\n");
           }
    }catch (Exception e) {
        e.printStackTrace();
    }
    return "File created"}

该文件将被创建,并且应包含列表中的以下条目

       header1  header2 header3 header4
        value1  value1  value1  value1
        value2  value2  value2  value2
        value3  value3  value3  value3
        value4  value4  value4  value4

答案 2 :(得分:-1)

以下代码解决了您的问题:

type