List combination map processing data The internal properties of the list object change when the value in the map changes

Need to process the table data during development and wrote the following code: Insert picture description here
but found that after executing name.put, she clearly did not operate on the List object listname, but she had data, which led to subsequent logic errors (the breakpoint test diagram is at the end of the article)

Then use the following code to test:

public static void main(String[] args) {
		List<Map> list = new ArrayList();
		Map map = new HashMap();
		list.add(map);
		map.put("aa", 11);
		map.put("bb", 11);
		map.put("cc", 11);
		list.forEach(m->System.out.println(m));
	}

Output result:
Insert picture description here
After searching on the Internet, I found the reason:
In addition to the basic type of map, which is the value transfer of the eight classics, the rest are all reference address transfers.
Because the reference address transfer (list) stored in the value of the map occurs, when the internal properties of the list object occur When changing, the value in the map changes

Guess you like

Origin blog.csdn.net/Beatingworldline/article/details/113477407