[英]Write HashMap to Csv File
I have a hashmap with a String key and List(String) as values. 我有一个带有字符串键和List(String)作为值的哈希图。
For Example: 例如:
Key | Values
John | GroupName1,GroupName2,GroupName3
Nick | Groupname4,GroupName1
I would like to write this hashmap to a csv file such that my csv file contains rows as below: 我想将此哈希图写入一个csv文件,以便我的csv文件包含以下行:
John,GroupName1,GroupName2,GroupName3
Nick,Groupname4,GroupName1
I tried: 我试过了:
Map<String, List<String>> usersmap = (Map<String, List<String>>) model.get("users"); //this is my map
StringWriter output = new StringWriter();
ICsvListWriter listWriter;
listWriter = new CsvListWriter(output, CsvPreference.STANDARD_PREFERENCE);
for (Map.Entry<String, List<String>> entry : usersmap.entrySet()) {
listWriter.write(entry.getKey(), entry.getValue());
}
but it doesn't seem right to me. 但这对我来说似乎不对。
Try to convert your List into a comma separated string using String.join(...)
尝试使用
String.join(...)
将列表转换成逗号分隔的字符串
listWriter.write(entry.getKey(), String.join(",", entry.getValue());
Java documentation: Java文档:
https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#join-java.lang.CharSequence-java.lang.Iterable- https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#join-java.lang.CharSequence-java.lang.Iterable-
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.