2011-04-28 198 views
0

我读取两个CSV文件,方含一组属性搜索在散列表的键值对映射

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

我需要两个文件匹配,并为每一个匹配rollno我不得不追加文件2属性到File1中,并创建格式为 的CSV文件rollno,name,class,city,town

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

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

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

如何通过第一个文件的列表映射搜索第二个listmap中包含的roll no并将其附加到第一个listmap?

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

回答

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 

} 
+0

'List '?这肯定是一个错字。 'List'只有一个类型参数。 – 2011-04-28 13:09:18

+0

点采取,改变它 – leifg 2011-04-28 13:11:35

0

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

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] 
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; 
    } 

}