title: Java for loop executed twice
body: <p>I am experiencing some troubles when executing a for loop. The loop is called twice. Here is the code that does the work: </p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.List;
  
  public class PoolItemMapper {
      public  List&lt;Item&gt; mapJsonObjectsToItems(JsonResponse jsonResponse) {
          int count = 0;
          List&lt;Item&gt; itemsList = new ArrayList&lt;&gt;();
           List&lt;Item&gt; 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() &lt;=  0) {
              return oldItem;
          } else if (oldItem.getMembersReference().getItems().size() &gt; 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;
      }
  
  }
  </code></pre>
  
  <p>The list has a size of 134, so I receive an output of two times 'Call count 134'. This results in having duplicates in the list.<br>
  Here are the POJOs:  </p>
  
  <p>JSON response where <code>getItems()</code> for the foor loop is called:  </p>
  
  <pre><code>public class JsonResponse {
      private String kind;
      private String selfLink;
      private List&lt;Item&gt; items = new ArrayList&lt;Item&gt;();
  
      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&lt;Item&gt; getItems() {
          return items;
      }
  
      public void setItems(List&lt;Item&gt; items) {
          this.items = items;
      }
  }
  </code></pre>
  
  <p>The <code>Item</code> class is a simple DTO, containing only variables and their getters/setters:<br>
  Here is where the method is invoked:  </p>
  
  <pre><code>itemTree = new PoolTreeBuilderImpl().buildTree(j);
  
  itemTree.stream().forEach(i -&gt; {
      System.out.println("[PARENT] " + i.getData().toString());
      i.getData().getSubItems().stream().forEach(si -&gt; {
          System.out.println("       [CHILD] " + si.toString());
      });
  });
  </code></pre>
  
  <p>}</p>
  
  <p>and the <code>PoolTreeBuilderImpl</code> calls: </p>
  
  <pre><code>@Override
  public List&lt;TreeNode&lt;Item&gt;&gt; buildTree(JsonResponse jsonResponse) {
      List&lt;TreeNode&lt;Item&gt;&gt; itemTree = new ArrayList&lt;&gt;();
      List&lt;Item&gt; mappedItems = new PoolItemMapper().mapJsonObjectsToItems(jsonResponse);
      for (Item i : mappedItems) {
          TreeNode&lt;Item&gt; item = new TreeNode&lt;&gt;(i);
          if (i.getSubItems().size() &gt; 0) {
              for (Item subItem : i.getSubItems()) {
                  item.addChild(subItem);
              }
          }
          itemTree.add(item);
      }
      return itemTree;
  }
  </code></pre>
  
  <p>Could someone explain me why this loop is called twice resulting in having each subitem twice in the list?</p>
  
  <p><strong>Update</strong><br>
  When executing this code, I don't have the duplicates:  </p>
  
  <pre><code>List&lt;Item&gt; mappedItems = new PoolItemMapper().mapJsonObjectsToItems(jsonResponse);
  mappedItems.forEach(i -&gt; {
      System.out.println("[PARENT] " + i.toString());
      i.getMembersReference().getItems().forEach(s -&gt; {
          System.out.println("      [CHILD] " + s.toString());
      });
  });
  </code></pre>