2011-04-28 202 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; 
    } 

}