在解析 csv 文件时,我将每一列添加到列表中,然后将每个列表添加到 map
中。在每个映射中,键是列表的第一行。
CSVReader reader = new CSVReader(new FileReader("plik.csv"),';');
CSVReader ile = new CSVReader(new FileReader("plik.csv"),';');
int columnCount=0;
String[] header = ile.readNext(); // assuming first read
if (header != null) { // and there is a (header) line
columnCount = header.length; // get the column count
}
HashMap<String, List<String>> map = new HashMap<String, List<String>>();
List content = reader.readAll();
String[] row;
String key;
int column=0;
for(int i=0; i <columnCount-1; i++) {
List<String> lista = new ArrayList<String>();
key=null;
for (Object object : content) {
row = (String[]) object;
if (column== 0) {
key = row[i];
}
if (kolumna != 0) {
lista.add(row[i]);
map.put(key, lista);
}
kolumna++;
}
kolumna=0;
}
在此操作之后,我希望能够访问每个列表上迭代的可能性。
我想使用来做到这一点
for (String q: map.keySet ()) {
System.out.println (q + "-" + map.get (q));
}
但结果是每个列表都显示在每一行的 szytwno 上。我无法访问列表中我想抛出的单个项目,例如。到下拉 Swing 。
输出:
Datum - [07.10.2014, 07.10.2014, 07.10.2014]
ProzessID - [1180804, 1180804, 335605]
key :数据
、ProzessID
最佳答案
尝试使用map.entrySet()代替:
for (Entry<String, List<String>> entry : map.entrySet())
{
System.out.println(entry.getKey() + ":");
for (String s : entry.getValue()){
System.out.println(s);
}
}
关于Java访问列表,这是在 map 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27094410/