我正在阅读两个包含一组属性的csv文件
File 1 attributes = name, class, rollno,
File 2 attributes = rollno, city,town
我需要匹配这两个文件,对于每个匹配的rollno,我必须将File 2属性附加到File1中并以格式创建一个csv文件 rollno,名字,类,城市,城镇
到目前为止,我已成功将File1和file2值读入该类型的链接哈希映射列表中。
List<Map<Object, Object>> _students = new ArrayList<Map<Object, Object>>();
我无法弄清楚前进的步骤。
如何在第一个文件的列表地图中搜索第二个listmap中包含的roll no并将其附加到firstlistmap?
然后按照指定的顺序将其打印到csv文件中(我能够按照它们在地图中插入的顺序迭代并打印所有值)
答案 0 :(得分:1)
我会通过读取第一个文件并将值存储在hashmap中来解决此问题。然后附加到该hashmap 关键是角色编号,值将是其他值的列表。
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
}
答案 1 :(得分:0)
将第二个文件加载到由rollno
键入的地图中。这样,您可以使用get()
方法查找与rollno匹配的详细信息。
答案 2 :(得分: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]
答案 3 :(得分: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;
}
}