如何在使用map时避免覆盖for循环中的List(Object[])部分。
huangapple go评论76阅读模式
英文:

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 &lt;Object []&gt; 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

view image


This is the method where the received list is transformed into a list type Map <String, Object>

@GetMapping(&quot;/teacher-social-media3&quot;)
public ResponseEntity&lt;?&gt; sqlObjectNative(){
	
	List&lt;Object[]&gt; listRecibid = teacherSocialMediaServ.sqlObjectMany();
	List&lt;Object&gt; targetList = new LinkedList&lt;Object&gt;();		
	
	Map&lt;String, Object&gt; map = new LinkedHashMap&lt;String, Object&gt;(); /*Map&lt;String, Object&gt; map = new HashMap&lt;String, Object&gt;();*/			
	
	
    if(listRecibida != null &amp;&amp; listRecibida.size()&gt;0 ){	  
		for(Object[] object: listRecibida ) {	
			map.put(&quot;Id_teacher_social_media&quot;, object[0]);				
			map.put(&quot;Id_teacher_fk&quot;, object[1]);
			map.put(&quot;Id_social_media_fk&quot;, object[2]);
			map.put(&quot;Nickname&quot;, object[3]);
			map.put(&quot;Id_teacher&quot;, object[4]);
			map.put(&quot;Name_teacher&quot;, object[5]);
			map.put(&quot;Id_social_media&quot;, object[6]);
			map.put(&quot;getName&quot;, object[7]);
			map.put(&quot;Icon&quot;, object[8]);												
			
			targetList.add(map);
		}
	}else {
		map.put(&quot;MESSAJE&quot;, &quot;THERE ARE NO RECORDS ON BD&quot;);
		return new ResponseEntity&lt;Map&lt;String, Object&gt;&gt;(map, HttpStatus.NOT_FOUND);
	} //return new ResponseEntity&lt;Map&lt;String, Object&gt;&gt;(map, HttpStatus.OK);	    	    
	return new ResponseEntity&lt;&gt;(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 [] ..)

[
    {
        &quot;Id_teacher_social_media&quot;: 12,
        &quot;Id_teacher_fk&quot;: 6,
        &quot;Id_social_media_fk&quot;: 8,
        &quot;Nickname&quot;: &quot;gamers rela&quot;,
        &quot;Id_teacher&quot;: 6,
        &quot;Name_teacher&quot;: &quot;Manuelo&quot;,
        &quot;Id_social_media&quot;: &quot;man&quot;,
        &quot;getName&quot;: 8,
        &quot;Icon&quot;: &quot;VideoJuegos&quot;
    },
    {
        &quot;Id_teacher_social_media&quot;: 12,
        &quot;Id_teacher_fk&quot;: 6,
        &quot;Id_social_media_fk&quot;: 8,
        &quot;Nickname&quot;: &quot;gamers rela&quot;,
        &quot;Id_teacher&quot;: 6,
        &quot;Name_teacher&quot;: &quot;Manuelo&quot;,
        &quot;Id_social_media&quot;: &quot;man&quot;,
        &quot;getName&quot;: 8,
        &quot;Icon&quot;: &quot;VideoJuegos&quot;
    },
    {
        &quot;Id_teacher_social_media&quot;: 12,
        &quot;Id_teacher_fk&quot;: 6,
        &quot;Id_social_media_fk&quot;: 8,
        &quot;Nickname&quot;: &quot;gamers rela&quot;,
        &quot;Id_teacher&quot;: 6,
        &quot;Name_teacher&quot;: &quot;Manuelo&quot;,
        &quot;Id_social_media&quot;: &quot;man&quot;,
        &quot;getName&quot;: 8,
        &quot;Icon&quot;: &quot;VideoJuegos&quot;
    },
    {
        &quot;Id_teacher_social_media&quot;: 12,
        &quot;Id_teacher_fk&quot;: 6,
        &quot;Id_social_media_fk&quot;: 8,
        &quot;Nickname&quot;: &quot;gamers rela&quot;,
        &quot;Id_teacher&quot;: 6,
        &quot;Name_teacher&quot;: &quot;Manuelo&quot;,
        &quot;Id_social_media&quot;: &quot;man&quot;,
        &quot;getName&quot;: 8,
        &quot;Icon&quot;: &quot;VideoJuegos&quot;
    },
    {
        &quot;Id_teacher_social_media&quot;: 12,
        &quot;Id_teacher_fk&quot;: 6,
        &quot;Id_social_media_fk&quot;: 8,
        &quot;Nickname&quot;: &quot;gamers rela&quot;,
        &quot;Id_teacher&quot;: 6,
        &quot;Name_teacher&quot;: &quot;Manuelo&quot;,
        &quot;Id_social_media&quot;: &quot;man&quot;,
        &quot;getName&quot;: 8,
        &quot;Icon&quot;: &quot;VideoJuegos&quot;
    },
    {
        &quot;Id_teacher_social_media&quot;: 12,
        &quot;Id_teacher_fk&quot;: 6,
        &quot;Id_social_media_fk&quot;: 8,
        &quot;Nickname&quot;: &quot;gamers rela&quot;,
        &quot;Id_teacher&quot;: 6,
        &quot;Name_teacher&quot;: &quot;Manuelo&quot;,
        &quot;Id_social_media&quot;: &quot;man&quot;,
        &quot;getName&quot;: 8,
        &quot;Icon&quot;: &quot;VideoJuegos&quot;
    },
    {
        &quot;Id_teacher_social_media&quot;: 12,
        &quot;Id_teacher_fk&quot;: 6,
        &quot;Id_social_media_fk&quot;: 8,
        &quot;Nickname&quot;: &quot;gamers rela&quot;,
        &quot;Id_teacher&quot;: 6,
        &quot;Name_teacher&quot;: &quot;Manuelo&quot;,
        &quot;Id_social_media&quot;: &quot;man&quot;,
        &quot;getName&quot;: 8,
        &quot;Icon&quot;: &quot;VideoJuegos&quot;
    },
    {
        &quot;Id_teacher_social_media&quot;: 12,
        &quot;Id_teacher_fk&quot;: 6,
        &quot;Id_social_media_fk&quot;: 8,
        &quot;Nickname&quot;: &quot;gamers rela&quot;,
        &quot;Id_teacher&quot;: 6,
        &quot;Name_teacher&quot;: &quot;Manuelo&quot;,
        &quot;Id_social_media&quot;: &quot;man&quot;,
        &quot;getName&quot;: 8,
        &quot;Icon&quot;: &quot;VideoJuegos&quot;
    },
    {
        &quot;Id_teacher_social_media&quot;: 12,
        &quot;Id_teacher_fk&quot;: 6,
        &quot;Id_social_media_fk&quot;: 8,
        &quot;Nickname&quot;: &quot;gamers rela&quot;,
        &quot;Id_teacher&quot;: 6,
        &quot;Name_teacher&quot;: &quot;Manuelo&quot;,
        &quot;Id_social_media&quot;: &quot;man&quot;,
        &quot;getName&quot;: 8,
        &quot;Icon&quot;: &quot;VideoJuegos&quot;
    },
    {
        &quot;Id_teacher_social_media&quot;: 12,
        &quot;Id_teacher_fk&quot;: 6,
        &quot;Id_social_media_fk&quot;: 8,
        &quot;Nickname&quot;: &quot;gamers rela&quot;,
        &quot;Id_teacher&quot;: 6,
        &quot;Name_teacher&quot;: &quot;Manuelo&quot;,
        &quot;Id_social_media&quot;: &quot;man&quot;,
        &quot;getName&quot;: 8,
        &quot;Icon&quot;: &quot;VideoJuegos&quot;
    },
    {
        &quot;Id_teacher_social_media&quot;: 12,
        &quot;Id_teacher_fk&quot;: 6,
        &quot;Id_social_media_fk&quot;: 8,
        &quot;Nickname&quot;: &quot;gamers rela&quot;,
        &quot;Id_teacher&quot;: 6,
        &quot;Name_teacher&quot;: &quot;Manuelo&quot;,
        &quot;Id_social_media&quot;: &quot;man&quot;,
        &quot;getName&quot;: 8,
        &quot;Icon&quot;: &quot;VideoJuegos&quot;
    },
    {
        &quot;Id_teacher_social_media&quot;: 12,
        &quot;Id_teacher_fk&quot;: 6,
        &quot;Id_social_media_fk&quot;: 8,
        &quot;Nickname&quot;: &quot;gamers rela&quot;,
        &quot;Id_teacher&quot;: 6,
        &quot;Name_teacher&quot;: &quot;Manuelo&quot;,
        &quot;Id_social_media&quot;: &quot;man&quot;,
        &quot;getName&quot;: 8,
        &quot;Icon&quot;: &quot;VideoJuegos&quot;
    }
]

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.

huangapple
  • 本文由 发表于 2020年5月5日 09:38:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/61604353.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定

  • 开发者交流平台

    本页二维码