title: Combine 2 object lists by index and retain key and value even if there are duplicates
body: <p>Have an object which has 2 lists.
  <br/>Is there a way I could merge the 2 lists by index and still retain
  key / values if there is a conflict between the 2 lists?</p>
  <p>This is current object with 2 lists. The lists are not always of same length.</p>
  <pre><code>{
      &quot;listA&quot;: [
          {
              &quot;text&quot;: &quot;index 0 list 1 text&quot;,
              &quot;icon&quot;: &quot;index 0 list 1 icon&quot;
          },
          {
              &quot;text&quot;: &quot;index 1 list 1 text&quot;,
              &quot;icon&quot;: &quot;index 1 list 1 icon&quot;
          }
      ],
      &quot;listB&quot;: [
          {
              &quot;text&quot;: &quot;index 0 list 2 text&quot;
          },
          {
              &quot;title&quot;: &quot;index 1 list 2 title&quot;,
              &quot;text&quot;: &quot;index 1 list 2 text&quot;,
              &quot;icon&quot;: &quot;index 1 list 2 icon&quot;
          },
          {
              &quot;title&quot;: &quot;index 2 list 2 title 2&quot;,
              &quot;text&quot;: &quot;index 2 list 2 text 2&quot;,
              &quot;icon&quot;: &quot;index 2 list 2 icon 2&quot;
          },
          {
              &quot;header&quot;: &quot;index 3 list 2 header 2&quot;,
              &quot;icon&quot;: &quot;index 3 list 2 icon 2&quot;
          }
      ]
  }
  </code></pre>
  <p>After merging looking to achieve following. (A new list)</p>
  <pre><code>{
      &quot;listC&quot;: [
          {
              &quot;text&quot;: &quot;index 0 list 1 text&quot;,
              &quot;icon&quot;: &quot;index 0 list 1 icon&quot;,
              &quot;text&quot;: &quot;index 0 list 2 text&quot;
          },
          {
              &quot;text&quot;:  &quot;index  1 list 1 text&quot;,
              &quot;icon&quot;:  &quot;index  1 list 1 icon&quot;,
              &quot;title&quot;: &quot;index  1 list 2 title&quot;,
              &quot;text&quot;:  &quot;index  1 list 2 text&quot;,
              &quot;icon&quot;:  &quot;index  1 list 2 icon&quot;
          },
          {
              &quot;title&quot;: &quot;index  2 list 2 title 2&quot;,
              &quot;text&quot;:  &quot;index  2 list 2 text  2&quot;,
              &quot;icon&quot;:  &quot;index  2 list 2 icon  2&quot;
          },
          {
              &quot;header&quot;: &quot;index 3 list 2 header 2&quot;,
              &quot;icon&quot;:   &quot;index 3 list 2 icon   2&quot;
          }
      ]
  }
  </code></pre>
  <p>Understand this is not feasible due to duplicate keys.
  Thus one thought is to change the names for the keys in list 2 when merging.</p>
  <p>Something like key <code>text</code> becomes <code>shortCopy</code> for list 1 and <code>longCopy</code> for list 2.</p>
  <p>Thus resulting in:</p>
  <pre><code>{
      &quot;shortCopy&quot;:  &quot;index 0 list 1 text&quot;,
      &quot;icon&quot;:  &quot;index 0 list 1 icon&quot;,
  
      &quot;longCopy&quot;: &quot;index 0 list 2 text&quot;
  }
  </code></pre>
  <p>Is there an elegant way to achieve this via a stream?</p>
  <p>Attempted following where I am creating a new object that holds different keys and adding that to a separate list.
  <br/>Resulting in no conflicts.</p>
  <p>Overall the question is, is there a more elegant approach to achieving this? Possibly with a Stream?
  <br /> As can see, this involved multiple loops and conditionals and the need to create a a new list and object.</p>
  <p>Looking for a cleaner solution.</p>
  <p>My attempt to achieve this.</p>
  <pre><code>MyObj myObj = // some REST call;
  List&lt;NewBenefit&gt; newBenefits = new ArrayList&lt;&gt;();
  
  List&lt;InitialBenefit&gt; list1 = myObj.getList1();
  List&lt;InitialBenefit&gt; list2 = myObj.getList2();
  
  List&lt;Benefit&gt; newBenefits = new ArrayList&lt;&gt;();
  
  // just adding the list 1 key/values to new list first. 
  for (InitialBenefit item : list1) {
      Benefit benefit = new Benefit();
      benefit.setIcon(item.getIcon());
      benefit.setShortCopy(item.getText());
      newBenefits.add(benefit);
  }
  
  for (int i = 0; i &lt; list2.size(); i++) {
      // newBenefits list now exists with some objects. 
      // so check if add new object or update keys in existing object
      // within the list
      if (newBenefits.size() &gt; i) {
          Benefit benefit = newBenefits.get(i);
          benefit.setIcon(list2.get(i).getIcon());
          benefit.setLongCopy(list2.get(i).getText());
      } else {
          Benefit benefit = new Benefit();
          benefit.setIcon(list2.get(i).getIcon());
          benefit.setLongCopy(list2.get(i).getText());
          newBenefits.add(benefit);
      }
  }
  </code></pre>