0

我正在阅读两个包含一组属性的 csv 文件

File 1 attributes = name, class, rollno,   
File 2 attributes = rollno, city,town

我需要匹配这两个文件,对于每个匹配的 rollno,我必须将 File 2 属性附加到 File1 中,并以 rollno、name、class、city、town 格式创建一个 csv 文件

到目前为止,我已成功将 File1 和 file2 值读入该类型的链接哈希图列表中。

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

我无法弄清楚前进的步骤。

如何在第一个文件的列表映射中搜索第二个列表映射中包含的卷号并将其附加到第一个列表映射?

然后按指定的顺序将其打印到 csv 文件中(我能够按照它们在地图中插入的顺序迭代和打印所有值)

4

4 回答 4

1

我会通过读取第一个文件并将值存储在哈希图中来解决这个问题。然后附加到该哈希映射键将是角色编号,值将是其他值的列表。

Map<String, List<String>> map = new HashMap<String, List<String>>()

Pseudocode:

for (List<String> line : file1.lines) {
 List curLine = new LinkedList();
 curLine.add(line.get(0));
 curLine.add(line.get(1));

 map.put(line.get(2),curLine)
}

for (List<String> line : file2.lines) {
 String key = line.get(0);
 String list = map.get(key);
 if (list != null)
 {
  list.add(line.get(1));
  list.add(line.get(2));
 }

 map.put(key,list); // probably not necessary as you change the reference that is already in the map, but I'm not sure

}
于 2011-04-28T13:04:10.270 回答
0

将第二个文件加载到由键控的 Map 中,rollno这样您可以使用该get()方法查找与 rollno 匹配的详细信息。

于 2011-04-28T12:55:43.880 回答
0

假设您设法将两个文件的内容加载到实际的数据结构中:

List<String[]> file1 = loadFile1();   // each item is a String array {name, class, rollNo}
List<String[]> file2 = loadFile2();   // each item is a String array {rollNo, city, town}

然后创建一个这样的地图:

// sorted map
Map<String, String[]> result = new TreeMap<String, String[]>();

// create a map entry for each roll nr of first file, add name and class
for (String[] file1Item : file1) {
  result.put(file1Item[0], new String[4] {file1Item[1], file2Item[2], "", ""});
}

// add the values from list 2 
for (String[] file2Item : file2) {
  String[] value = result.get(file2Item[2]);
  if (value != null) {
     value[2] = file2Item[1];
     value[3] = file2Item[2];
  }
}

现在你有一张这样的地图:

rollno -> [name, class, city, town]
于 2011-04-28T13:08:10.063 回答
0

试试这个完整的代码它会工作

import java.util.HashMap;
import java.util.Map;

import com.csvreader.CsvReader;

public class ListMap {
    public static void main(String args[]) throws Exception {
        Map<String, ListMap> map = new HashMap<String, ListMap>();
        CsvReader reader = null;
        reader = new CsvReader("c:/list1.csv");
        reader.readHeaders();
        while (reader.readRecord()) {
            map.put(reader.get("RollNo"), new ListMap(reader.get("RollNo"),
                    reader.get("Name"), reader.get("Class"),
                    reader.get("City"), reader.get("Town")));
        }
        reader = new CsvReader("c:/list2.csv");
        reader.readHeaders();
        while (reader.readRecord()) {
            ListMap obj = map.get(reader.get("RollNo"));
            if (obj != null) {
                obj.setCity(reader.get("City"));
                obj.setTown(reader.get("Town"));
            } else {
                obj = new ListMap(reader.get("RollNo"), reader.get("Name"),
                        reader.get("Class"), reader.get("City"), reader
                                .get("Town"));
            }
            map.put(reader.get("RollNo"), obj);
        }
        for (Map.Entry<String, ListMap> entry : map.entrySet()) {
            ListMap obj = entry.getValue();
            System.out.println(entry.getKey() + " " + obj.name + " "
                    + obj.className + " " + obj.city + " " + obj.town);
        }

    }

    private String roolNo, name, className, city, town;

    public ListMap(String roolNo, String name, String className, String city,
            String town) {
        super();
        this.roolNo = roolNo;
        this.name = name;
        this.className = className;
        this.city = city;
        this.town = town;
    }

    public String getRoolNo() {
        return roolNo;
    }

    public void setRoolNo(String roolNo) {
        this.roolNo = roolNo;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getClassName() {
        return className;
    }

    public void setClassName(String className) {
        this.className = className;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getTown() {
        return town;
    }

    public void setTown(String town) {
        this.town = town;
    }

}
于 2011-04-28T13:44:07.607 回答