将哈希图中的数据作为CSV文件写入 [英] write data from hashmap as a CSV file
问题描述
说我有一个哈希表,其中 String
类型为键,而 ArrayList
类型为值,例如 {"value1" = ["one","3",五个"],值2" = [两个",四个",六个"]}
,其中值1"和值2"是键.我想以下面的格式写上面的哈希图数据.(以便我可以在excel中读取csv文件)
Say I have a hashmap with String
type as key and ArrayList
type as value, example {"value1"=["one","three","five"], "value2"=["two","four","six"]}
where "value1" and "value2" are keys. I want to write the above hashmap data in following format. (so that I can read the csv file in excel)
value1,value2
one,two
three,four
five,six
我的想法是按如下方式编写第一个键及其值
My idea was to write the first key and its values as follows
value1
one
three
five
然后我正在考虑使用 RandomAccessFile
类中的seek方法返回第1行,然后再次写入第二个键及其值.但是,我无法完成此任务,因为seek函数会吸收整个文件中字符串的长度,然后在其后写入新字符串.当我希望指针跳到第一行并追加字符串时.有一个更好的方法吗?.一个快速的例子将不胜感激.
Then I was thinking of using the seek method in RandomAccessFile
class to back to line 1 and again write the second key and its values. However I am not able to accomplish this task since seek function takes in the length of strings in the entire file and writes the new string after it. While I wanted the pointer to jump to the first line and append the string. Is there a better way to do this?. A quick example would be much appreciated.
谢谢
推荐答案
为什么不能只使用4个字符串,每行一个?像这样:
Why can't you just use 4 Strings, one for each row? Something like this:
StringBuilder keyRow = new StringBuilder();
StringBuilder value1 = new StringBuilder();
StringBuilder value2 = new StringBuilder();
StringBuilder value3 = new StringBuilder();
Iterator keys = hashmap.keySet().iterator();
boolean notFirst = true;
while(keys.hasNext()) {
String key = (String)keys.next();
ArrayList list = (ArrayList)hashmap.get(key);
if(!notFirst) {
keyRow.append(",");
value1.append(",");
value2.append(",");
value3.append(",");
}
keyRow.append(key);
value1.append((String)list.get(0));
value2.append((String)list.get(1));
value3.append((String)list.get(2));
notFirst = false;
}
然后最后,取4个字符串
Then at the end, just take the 4 Strings
String csv = keyRow.toString()+"\n"+value1.toString()+"\n"+value2.toString()+"\n"+value3.toString();
请注意,此示例并不是真正正确的CSV.带有逗号的字符串不会用引号引起来.
Note that this example isn't really proper CSV. Strings with commas aren't wrapped in quotes.
或者,如果您有成千上万的此类行,则可以对HashMap进行一千次迭代.为了节省查找键的时间,您可以将它们全部放入 ArrayList
:
Or you iterate through the HashMap a thousand times if you have thousands of these rows. To save a bit of time from looking up a key, you can put them all in an ArrayList
:
StringBuilder csv = new StringBuilder();
int row = 0;
ArrayList<ArrayList> list = new ArrayList<ArrayList>();
// Write the keys row:
Iterator keys = hashmap.keySet().iterator();
boolean notFirst = true;
while(keys.hasNext()) {
String key = (String)keys.next();
ArrayList tmp = (ArrayList)hashmap.get(key);
if(!notFirst) {
csv.append(",");
}
csv.append(key);
// store list
list.add(tmp);
notFirst = false;
}
csv.append("\n");
// Write the rest of the rows
while(row<numberOfTotalRow) {
notFirst = true;
for(int x=0;x<list.size();x++) {
if(!notFirst) {
csv.append(",");
}
csv.append((String)list.get(x).get(row));
notFirst = false;
}
row++;
}
这篇关于将哈希图中的数据作为CSV文件写入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!