英文:
How to avoid overwriting a List <Object> inside a for (Object []) with map
问题
Sure, here is the translated content:
@GetMapping("/teacher-social-media3")
public ResponseEntity<?> sqlObjectNative(){
List<Object[]> receivedList = teacherSocialMediaServ.sqlObjectMany();
List<Object> targetList = new LinkedList<Object>();
Map<String, Object> map = new LinkedHashMap<String, Object>(); /*Map<String, Object> map = new HashMap<String, Object>();*/
if(receivedList != null && receivedList.size() > 0 ){
for(Object[] object: receivedList ) {
map.put("Id_teacher_social_media", object[0]);
map.put("Id_teacher_fk", object[1]);
map.put("Id_social_media_fk", object[2]);
map.put("Nickname", object[3]);
map.put("Id_teacher", object[4]);
map.put("Name_teacher", object[5]);
map.put("Id_social_media", object[6]);
map.put("getName", object[7]);
map.put("Icon", object[8]);
targetList.add(map);
}
}else {
map.put("MESSAJE", "THERE ARE NO RECORDS ON BD");
return new ResponseEntity<Map<String, Object>>(map, HttpStatus.NOT_FOUND);
} //return new ResponseEntity<Map<String, Object>>(map, HttpStatus.OK);
return new ResponseEntity<>(targetList, HttpStatus.OK);
}
Please note that the translation might not be perfect due to potential limitations in automatic translation and context interpretation. If you have any questions or need further assistance, feel free to ask!
英文:
I have a list type List <Object []>
that is dynamically loaded from a database query. How can you correct and avoid that with each iteration of the For (Object [] ..)
cycle, the data already entered is overwritten by the last value of the map list in targetList.add (map);
This is the result of the query, which I am loading from the database
This is the method where the received list is transformed into a list type Map <String, Object>
@GetMapping("/teacher-social-media3")
public ResponseEntity<?> sqlObjectNative(){
List<Object[]> listRecibid = teacherSocialMediaServ.sqlObjectMany();
List<Object> targetList = new LinkedList<Object>();
Map<String, Object> map = new LinkedHashMap<String, Object>(); /*Map<String, Object> map = new HashMap<String, Object>();*/
if(listRecibida != null && listRecibida.size()>0 ){
for(Object[] object: listRecibida ) {
map.put("Id_teacher_social_media", object[0]);
map.put("Id_teacher_fk", object[1]);
map.put("Id_social_media_fk", object[2]);
map.put("Nickname", object[3]);
map.put("Id_teacher", object[4]);
map.put("Name_teacher", object[5]);
map.put("Id_social_media", object[6]);
map.put("getName", object[7]);
map.put("Icon", object[8]);
targetList.add(map);
}
}else {
map.put("MESSAJE", "THERE ARE NO RECORDS ON BD");
return new ResponseEntity<Map<String, Object>>(map, HttpStatus.NOT_FOUND);
} //return new ResponseEntity<Map<String, Object>>(map, HttpStatus.OK);
return new ResponseEntity<>(targetList, HttpStatus.OK);
}
As you can see the result of the list, all values are overwritten by the last one that had the map list inside the For cycle (Object [] ..)
[
{
"Id_teacher_social_media": 12,
"Id_teacher_fk": 6,
"Id_social_media_fk": 8,
"Nickname": "gamers rela",
"Id_teacher": 6,
"Name_teacher": "Manuelo",
"Id_social_media": "man",
"getName": 8,
"Icon": "VideoJuegos"
},
{
"Id_teacher_social_media": 12,
"Id_teacher_fk": 6,
"Id_social_media_fk": 8,
"Nickname": "gamers rela",
"Id_teacher": 6,
"Name_teacher": "Manuelo",
"Id_social_media": "man",
"getName": 8,
"Icon": "VideoJuegos"
},
{
"Id_teacher_social_media": 12,
"Id_teacher_fk": 6,
"Id_social_media_fk": 8,
"Nickname": "gamers rela",
"Id_teacher": 6,
"Name_teacher": "Manuelo",
"Id_social_media": "man",
"getName": 8,
"Icon": "VideoJuegos"
},
{
"Id_teacher_social_media": 12,
"Id_teacher_fk": 6,
"Id_social_media_fk": 8,
"Nickname": "gamers rela",
"Id_teacher": 6,
"Name_teacher": "Manuelo",
"Id_social_media": "man",
"getName": 8,
"Icon": "VideoJuegos"
},
{
"Id_teacher_social_media": 12,
"Id_teacher_fk": 6,
"Id_social_media_fk": 8,
"Nickname": "gamers rela",
"Id_teacher": 6,
"Name_teacher": "Manuelo",
"Id_social_media": "man",
"getName": 8,
"Icon": "VideoJuegos"
},
{
"Id_teacher_social_media": 12,
"Id_teacher_fk": 6,
"Id_social_media_fk": 8,
"Nickname": "gamers rela",
"Id_teacher": 6,
"Name_teacher": "Manuelo",
"Id_social_media": "man",
"getName": 8,
"Icon": "VideoJuegos"
},
{
"Id_teacher_social_media": 12,
"Id_teacher_fk": 6,
"Id_social_media_fk": 8,
"Nickname": "gamers rela",
"Id_teacher": 6,
"Name_teacher": "Manuelo",
"Id_social_media": "man",
"getName": 8,
"Icon": "VideoJuegos"
},
{
"Id_teacher_social_media": 12,
"Id_teacher_fk": 6,
"Id_social_media_fk": 8,
"Nickname": "gamers rela",
"Id_teacher": 6,
"Name_teacher": "Manuelo",
"Id_social_media": "man",
"getName": 8,
"Icon": "VideoJuegos"
},
{
"Id_teacher_social_media": 12,
"Id_teacher_fk": 6,
"Id_social_media_fk": 8,
"Nickname": "gamers rela",
"Id_teacher": 6,
"Name_teacher": "Manuelo",
"Id_social_media": "man",
"getName": 8,
"Icon": "VideoJuegos"
},
{
"Id_teacher_social_media": 12,
"Id_teacher_fk": 6,
"Id_social_media_fk": 8,
"Nickname": "gamers rela",
"Id_teacher": 6,
"Name_teacher": "Manuelo",
"Id_social_media": "man",
"getName": 8,
"Icon": "VideoJuegos"
},
{
"Id_teacher_social_media": 12,
"Id_teacher_fk": 6,
"Id_social_media_fk": 8,
"Nickname": "gamers rela",
"Id_teacher": 6,
"Name_teacher": "Manuelo",
"Id_social_media": "man",
"getName": 8,
"Icon": "VideoJuegos"
},
{
"Id_teacher_social_media": 12,
"Id_teacher_fk": 6,
"Id_social_media_fk": 8,
"Nickname": "gamers rela",
"Id_teacher": 6,
"Name_teacher": "Manuelo",
"Id_social_media": "man",
"getName": 8,
"Icon": "VideoJuegos"
}
]
Thank you very much for your opinions that you can share !!
答案1
得分: 1
每次在for循环开始时实例化一个新的HashMap。
英文:
Instantiate a new HashMap each time, at the beginning of the for loop.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论