Discussion: the difference between creating objects before and in a loop

Business scenario

The back end obtains data from the database and passes it to the front end.

Data format obtained by backend: List

Data format of front-end requirements: Json

[scenario analysis]

The data format obtained by the back end is List, while the data format required by the front end is Json. Therefore, the back end needs to reassemble the data into Json format to transmit it to the front end for reception. In the process of data reassembly, there will be such a problem. When objects are retrieved from the List one by one and put into another List, whether the intermediate object is created before or during the List loop.

These two different ways of creating objects result in two different assembly effects. And these two effects, one is right, the other is wrong.

[example code]

Code running environment

  • jdk: 1.8
  • Plug in: lombok
  • Framework: springboot

Item entity

/**
 * Item object
 */
@Data
public class Item {

    private String ItemId;
    private String ItemName;

}

Circular class

import java.util.ArrayList;
import java.util.List;

public class CreateObject{
    public static void main(String[] args) {
        //Initialization data
        List<Item> itemListIn = new ArrayList<>();

        Item item_1 = new Item();
        item_1.setItemId("1");
        item_1.setItemName("item_1");

        Item item_2 = new Item();
        item_2.setItemId("2");
        item_2.setItemName("item_2");

        Item item_3 = new Item();
        item_3.setItemId("3");
        item_3.setItemName("item_3");

        itemListIn.add(item_1);
        itemListIn.add(item_2);
        itemListIn.add(item_3);

        //Create objects outside the loop
        List<Item> itemListOut_t1 = new ArrayList<>();
        //Create the intermediate object item 'T1
        Item item_t1 = new Item();
        for (Item itemIn : itemListIn) {
            item_t1.setItemId(itemIn.getItemId());
            item_t1.setItemName(itemIn.getItemName());
            itemListOut_t1.add(item_t1);
        }

        System.out.println("Create object outside loop");
        System.out.println("After the end of the cycle, the data passed to the front end:");
        System.out.println(itemListOut_t1);

        //Create objects inside a loop
        List<Item> itemListOut_t2 = new ArrayList<>();
        for (Item itemIn : itemListIn) {
            //Create the intermediate object item't2
            Item item_t2 = new Item();
            item_t2.setItemId(itemIn.getItemId());
            item_t2.setItemName(itemIn.getItemName());
            itemListOut_t2.add(item_t2);
        }

        System.out.println("Create object inside loop");
        System.out.println("After the end of the loop, the data passed to the front end:");
        System.out.println(itemListOut_t2);
    }
}

Operation result

Code analysis

It is easy to see from the console that it is wrong to create objects outside the loop and right to create objects inside the loop.

The reasons for the two different output effects are as follows:

Create an object outside the loop, so that the same object is always used inside the loop. Although itemListIn has been looped three times, and item has been added to itemlistout \ \ T1 three times, all three times are the same object. In other words, the last three objects obtained by itemlistout \ \ T1 are the same.

While creating objects inside the loop uses three different objects. Finally, itemlistout ﹣ T1 gets three different objects.

Keywords: Java JSON Database JDK

Added by fesan on Thu, 05 Dec 2019 15:31:56 +0200