HashMap Print Results Puzzle Java

Source: Internet
Author: User

I had a minor change in the system maintenance, but I had an unexpected mistake, very confused,

The original program example is as follows:

for (int i =0;i<resultlist.size (); i++) {Map resultmap = new HashMap (); resultmap = (MAP) resultlist.get (i); resultgetlis T.add (RESULTMAP); } System.out.println ("resultgetlist =" +resutlgetlist);

In order to improve the efficiency of the program I think that the second line should not be put into the loop inside, because put it in the loop every time the new object, affect the speed, so I improve after the following:

Map Resultmap = new HashMap ();
for (int i =0;i<resultlist.size (); i++) {Resultmap = (MAP) resultlist.get (i); Resultgetlist.add (Resultmap);} System.out.println ("resultgetlist =" +resutlgetlist); But the modified result is different from the expected result, but I do not know where the problem, looking for a long time also did not find out, modify the program after the output List is always the last map value of the repeat, this let me very confused, I guess, List.add, is added to the object, or is the address of memory, The value of the map is not saved.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.