开发者

search for a key value pair in a list of hash maps

I am reading two csv files containg a set of attributes

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

I need to match the two files and for every matchi开发者_运维技巧ng rollno I have to append the File 2 attributes into File1 and create a csv file in the format rollno, name, class, city, town

So far I have succeeded in reading the File1 and file2 values into a List of linked hashmaps of the type.

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

I am not able to figure out the steps to move forward.

How do I search through the list map of first file for the roll no contained in the second listmap and append it to the firstlistmap?

and then print it to a csv file in the order specified ( I am able to iterate and print all the values in the order they were inserted in the map)


I would approach this problem by reading the first file and store the values in a hashmap. and then append to that hashmap The key would be the role number and the value would be a list of the the other values.

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

}


Load the second file into a Map keyed by rollno This way you can lookup the details which match a rollno using the get() method.


Assuming, you managed to load the contents of both files into practical datastructures:

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}

then create a map like this:

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

Now you have a map like this:

rollno -> [name, class, city, town]


Try this complete code it will work

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

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜