【问题标题】:How to print a list of maps with each map entry in a seperate line如何打印地图列表,每个地图条目在单独的行中
【发布时间】:2019-01-14 05:45:26
【问题描述】:

我想打印一个数组列表,在该数组列表中它包含哈希映射,我打印在一个文本文件中。在尝试打印它以单行打印的文本文件时。我需要在一个单独的行 示例:

ArrayList<Object> mainlist= new ArrayList<>();
LinkedHashMap<String, String> hmap = new LinkedHashMap<>();
for (int i=0;i<5;i++) 
{   
    hmap.put("key1", "03-08-2018");
    hmap.put("key2", "xyz";
    hmap.put("key3", "123");
    //ArrayList USed:::
    mainlist.add(hmap);
    mainlist.add("\n");
}
return mainlist;

我以文本文件的形式返回。它会生成一行,但我的输出文本文件中需要单独的行。

文本文件中的预期输出:

[{key1=03-08-2018,key2=xyz,key3=123}
 {key1=03-08-2018,key2=xyz,key3=123}
{key1=03-08-2018,key2=xyz,key3=123}
]

文本文件中的实际输出:

[{key1=03-08-2018,key2=xyz,key3=123},{key1=03-08-2018,key2=xyz,key3=123},{key1=03-08-2018,key2=xyz,key3=123}]

谁能帮我解决这个问题?
谢谢你的回复

【问题讨论】:

  • 不要只输出toString() 的结果,而是采用该结果并按照您想要的方式对其进行格式化。比如,用换行符替换,
  • 我没有得到你能解释一下吗
  • 您只需在列表上调用toString,您所看到的是列表的默认输出。您需要遍历列表并打印您喜欢什么以及喜欢什么的信息
  • 如何转储到文本文件中?可以分享一下代码吗?
  • 谢谢它的工作良好。

标签: java arrays arraylist hashmap linkedhashmap


【解决方案1】:

我测试了您的代码,它对我有用。您如何将其写入文件?

我是这样做的:

    Path path = Paths.get("/test/outputXXXX.txt");
    ArrayList<Object> mainlist= new ArrayList<>();
    LinkedHashMap<String, String> hmap = new LinkedHashMap<>();
    for (int i=0;i<5;i++)
    {
        hmap.put("key1", "03-08-2018");
        hmap.put("key2", "xyz");
        hmap.put("key3", "123");
        //ArrayList USed:::
        mainlist.add(hmap);
        mainlist.add("\n");
    }
    try (BufferedWriter writer = Files.newBufferedWriter(path))
    {
        writer.write(mainlist.toString());
    }

并在文件中输出:

[{key1=03-08-2018, key2=xyz, key3=123}, 
, {key1=03-08-2018, key2=xyz, key3=123}, 
, {key1=03-08-2018, key2=xyz, key3=123}, 
, {key1=03-08-2018, key2=xyz, key3=123}, 
, {key1=03-08-2018, key2=xyz, key3=123}, 
]

编辑: 抱歉,没有逗号,没看到你需要它。

你可以这样做:

    Path path = Paths.get("/test/outputXXXX.txt");
    ArrayList<Object> mainlist= new ArrayList<>();
    LinkedHashMap<String, String> hmap = new LinkedHashMap<>();
    for (int i=0;i<5;i++)
    {
        hmap.put("key1", "03-08-2018");
        hmap.put("key2", "xyz");
        hmap.put("key3", "123");
        //ArrayList USed:::
        mainlist.add(hmap);
        mainlist.add("\n");
    }
    try (BufferedWriter writer = Files.newBufferedWriter(path))
    {
        writer.write("[");
        for (Object o : mainlist) {
            writer.write(o.toString());
        }
        writer.write("]");
    }

【讨论】:

  • 感谢您的回复:我在控制台中得到了相同的输出,但我没有进入文本文件
  • 在控制台中它打印我们期望的内容,但不在文本文件中。它仍然单行打印
  • 你能展示你所有的代码吗?因为它对我有用:)
  • 感谢您的回复,当我在记事本++中打开时它工作正常
【解决方案2】:
ArrayList<Object> mainlist= new ArrayList<>();
LinkedHashMap<String, String> hmap = new LinkedHashMap<>();
for (int i=0;i<5;i++) 
{  
   hmap.put("key1", "03-08-2018");
   hmap.put("key2", "xyz"); //you missed closing paranthesis here
   hmap.put("key3", "123");
   //ArrayList USed:::
   mainlist.add(hmap);
   mainlist.add("\n");
}
return mainlist;

您可以在 :: https://www.tutorialspoint.com/compile_java_online.php 中尝试以下代码

import java.util.*;
public class ArrayListExample{
public static void main(String args[]){
    ArrayList<Object> mainlist= new ArrayList<>();
    LinkedHashMap<String, String> hmap = new LinkedHashMap<>();
    int i=0;
    for (i=0;i<5;i++) {  
        hmap.put("key1", "03-08-2018");
        hmap.put("key2", "xyz");
        hmap.put("key3", "123");
        //ArrayList USed:::
        mainlist.add(hmap);
        mainlist.add("\n");
    }
    System.out.println(mainlist);  
    return;
    }
}

这给出了您预期的输出。希望这对您有所帮助。 谢谢。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-30
    • 1970-01-01
    • 1970-01-01
    • 2015-10-05
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    • 1970-01-01
    相关资源
    最近更新 更多