Java List keeps adding the last record, duplicating it for the number of records in file

非 Y 不嫁゛ 提交于 2020-01-30 05:08:19

问题


A weird thing is happening in my code and I'm just not sure whats going on. I have a file that looks like this:

id;state;city;total_pop;avg_temp
1;Florida;;120000;76
2;Michigan;Detroit;330000;54
3;New Jersey;Newark;;34

My java parser should create a list of maps as the result and return. But the only thing that gets returned is the last record in the file repeated for the number of rows in the file. Can someone eye my code and enlighten me as to what is going on? Thank you in advance.

public class FileParserUtil {

    public List<Map<String, String>> parseFile(String fileName, char seperator)
            throws IOException {

        CSVReader reader = new CSVReader(new FileReader(fileName), seperator);
        Map<String, String> record = new HashMap<String, String>();
        List<Map<String, String>> rows = new ArrayList<Map<String, String>>();

        String[] header = reader.readNext();
        String[] nextLine;

        while ((nextLine = reader.readNext()) != null) {
            for (int i = 0; i < header.length; i++) {
                record.put(header[i], nextLine[i]);
            }
            System.out.println("--------Here is the record: ---------");
            System.out.println(record);
            rows.add(record);
            System.out.println("--------Here are the rows: ---------");
            System.out.println(rows);
        }
        reader.close();
        return rows;

    }
}

Here is the console output of the above run from a main method...

--------Here is the record: ---------
{id=1, avg_temp=76, state=Florida, total_pop=120000, city=}
--------Here are the rows: ---------
[{id=1, avg_temp=76, state=Florida, total_pop=120000, city=}]
--------Here is the record: ---------
{id=2, avg_temp=54, state=Michigan, total_pop=330000, city=Detroit}
--------Here are the rows: ---------
[{id=2, avg_temp=54, state=Michigan, total_pop=330000, city=Detroit}, {id=2, avg_temp=54, state=Michigan, total_pop=330000, city=Detroit}]
--------Here is the record: ---------
{id=3, avg_temp=34, state=New Jersey, total_pop=, city=Newark}
--------Here are the rows: ---------
[{id=3, avg_temp=34, state=New Jersey, total_pop=, city=Newark}, {id=3, avg_temp=34, state=New Jersey, total_pop=, city=Newark}, {id=3, avg_temp=34, state=New Jersey, total_pop=, city=Newark}]

回答1:


it is because in a HashMap you cannot have duplicate values.. so, when you do this

record.put("id","1");

it will check if there is already a key named "id", if it does it will replace its old value with the new value. At the first iteration it won't replace anything but from the next iteration it will start replacing the old values.

And when you add

row.add(record);

you are adding the same reference again and again, and since the map contains only the newly inserted value the same reference's toString() method is called and the same value is printed again and again.

you should have added

record = new HashMap<String,String>();

to add a new map for each record.




回答2:


I think that you forgot to replace record with new empty map, after it is added to list. You want something like this:

rows.add(record);
record = new HashMap<String, String>();



回答3:


while ((nextLine = reader.readNext()) != null) {
    Map<String, String> record = new HashMap<String, String>();
...
}

You are reusing the same instance of a map all the time, values get overwritten. I would also recommend to create a separate class for storing your data, it would be cleaner.




回答4:


You should move following line

Map<String, String> record = new HashMap<String, String>();

within for loop :

public class FileParserUtil {

    public List<Map<String, String>> parseFile(String fileName, char seperator)
            throws IOException {

        CSVReader reader = new CSVReader(new FileReader(fileName), seperator);
        List<Map<String, String>> rows = new ArrayList<Map<String, String>>();

        String[] header = reader.readNext();
        String[] nextLine;

        while ((nextLine = reader.readNext()) != null) {
            Map<String, String> record = new HashMap<String, String>();
            for (int i = 0; i < header.length; i++) {
                record.put(header[i], nextLine[i]);
            }
            System.out.println("--------Here is the record: ---------");
            System.out.println(record);
            rows.add(record);
            System.out.println("--------Here are the rows: ---------");
            System.out.println(rows);
        }
        reader.close();
        return rows;

    }
}


来源:https://stackoverflow.com/questions/19317940/java-list-keeps-adding-the-last-record-duplicating-it-for-the-number-of-records

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!