Reputation: 99
I have no idea why in every Value in my Map is putted the same last record. The Key is ok, but in every iteration my list is putted into everyone record in map. I dont understand why..
Could someone help ?
HashMap<Long, LinesEntity> xlsMapped = new HashMap<>();
MapEntity mapEntity = new MapEntity();
LinesEntity linesEntity = new LinesEntity();
ArrayList<String> list = new ArrayList<>();
//read first line
String line = br.readLine();
String array[];
long mapKey = 0;
while (line != null) {
array = line.split(",");
list.clear();
for (String cell : array) {
list.add(cell);
}
line = br.readLine();
linesEntity.setSingleLine(list);
dataService.saveOne(linesEntity);
xlsMapped.put(mapKey, linesEntity);
mapKey++;
}
// mapEntity.setMapa(xlsMapped);
// dataService.save(mapEntity);
}
Upvotes: 0
Views: 59
Reputation: 11832
I think you need to create new linesEntity
and list
object instances for each loop:
while (line != null) {
linesEntity = new LinesEntity(); // create a new LinesEntity for this loop execution
list = new ArrayList()
array = line.split(",");
Which means that technically you don't need to create them at the top, just declare them:
LinesEntity linesEntity;
ArrayList<String> list;
Upvotes: 3