Java for循环执行了两次

我在执行for循环时遇到了一些麻烦.循环被调用两次.以下是完成工作的代码:

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

public class PoolItemMapper {
    public  List<Item> mapJsonObjectsToItems(JsonResponse jsonResponse) {
        int count = 0;
        List<Item> itemsList = new ArrayList<>();
         List<Item> js = jsonResponse.getItems();
        for (Item item : jsonResponse.getItems()) {
            itemsList.add(addNormalItemProperties(item, new Item()));
            count++;
        }
        System.out.println("Call count: " + count);
        return itemsList;
    }

    private  Item addNormalItemProperties(Item oldItem, Item newItem) {
        if(oldItem.getMembersReference().getItems().size() <=  0) {
            return oldItem;
        } else if (oldItem.getMembersReference().getItems().size() > 0) {
            for (SubItem subItem: oldItem.getMembersReference().getItems()) {
                oldItem.getSubItems().add(creteNewSubItem(subItem));
            }
        }
        return oldItem;
    }

    private  Item creteNewSubItem(SubItem oldItem) {
        Item i = new Item();
        i.setDynamicRatio(oldItem.getDynamicRatio());
        i.setEphermal(oldItem.getEphermal());
        i.setInheritProfile(oldItem.getInheritProfile());
        i.setLogging(oldItem.getLogging());
        i.setRateLimit(oldItem.getRateLimit());
        i.setRatio(oldItem.getRatio());
        i.setSession(oldItem.getSession());
        i.setAddress(oldItem.getAddress());
        i.setName(oldItem.getName());
        i.setState(oldItem.getState());

        return i;
    }

}

该列表的大小为134,因此我收到两次“呼叫计数134”的输出.这导致列表中出现重复.
以下是POJO:

JSON响应,其中调用foor循环的getItems():

public class JsonResponse {
    private String kind;
    private String selfLink;
    private List<Item> items = new ArrayList<Item>();

    public JsonResponse() {

    }

    public String getKind() {
        return kind;
    }

    public void setKind(String kind) {
        this.kind = kind;
    }

    public String getSelfLink() {
        return selfLink;
    }

    public void setSelfLink(String selfLink) {
        this.selfLink = selfLink;
    }

    public List<Item> getItems() {
        return items;
    }

    public void setItems(List<Item> items) {
        this.items = items;
    }
}

Item类是一个简单的DTO,只包含变量及其getter / setter:
这是调用方法的地方:

itemTree = new PoolTreeBuilderImpl().buildTree(j);

itemTree.stream().forEach(i -> {
    System.out.println("[PARENT] " + i.getData().toString());
    i.getData().getSubItems().stream().forEach(si -> {
        System.out.println("       [CHILD] " + si.toString());
    });
});

}

和PoolTreeBuilderImpl调用:

@Override
public List<TreeNode<Item>> buildTree(JsonResponse jsonResponse) {
    List<TreeNode<Item>> itemTree = new ArrayList<>();
    List<Item> mappedItems = new PoolItemMapper().mapJsonObjectsToItems(jsonResponse);
    for (Item i : mappedItems) {
        TreeNode<Item> item = new TreeNode<>(i);
        if (i.getSubItems().size() > 0) {
            for (Item subItem : i.getSubItems()) {
                item.addChild(subItem);
            }
        }
        itemTree.add(item);
    }
    return itemTree;
}

有人可以解释为什么这个循环被调用两次导致每个子项在列表中两次?

更新
执行此代码时,我没有重复项:

List<Item> mappedItems = new PoolItemMapper().mapJsonObjectsToItems(jsonResponse);
mappedItems.forEach(i -> {
    System.out.println("[PARENT] " + i.toString());
    i.getMembersReference().getItems().forEach(s -> {
        System.out.println("      [CHILD] " + s.toString());
    });
});

最佳答案 问题出在JsonResponse对象上,它始终是相同的. JsonResponse列表中的对象被修改两次,因此存在重复项.这就是为什么(@Joakim Danielson)有第二个参数newItem.

另外,我不得不更改TreeBuilder的buildTree方法的签名,以接受由映射器返回的Items列表.

点赞