title: How to remove duplicate objects from a list based on a certain field but add a quantity before removing?
body: <p>I have an assembly class having multiple fields. </p>
  
  <p>I need to remove duplicate objects of the assembly class from a list of assembly objects based on assembly code, but before removing I need to add the assembly quantity to the matched assembly object's quantity.</p>
  
  <pre><code>package com.company;
  
  import java.util.ArrayList;
  import java.util.HashSet;
  import java.util.List;
  
  public class Main {
  
      public static void main(String[] args) {
          List&lt;Assembly&gt; assemblies = new ArrayList&lt;&gt;();
  
          assemblies.add(new Assembly("abc1", 3.0));
          assemblies.add(new Assembly("abc2", 6.0));
          assemblies.add(new Assembly("abc3", 8.4));
          assemblies.add(new Assembly("abc4", 9.0));
          assemblies.add(new Assembly("abc1", 4.2));
          assemblies.add(new Assembly("abc1", 6.3));
  
          System.out.println("List with duplicates: "+assemblies);
  
          HashSet&lt;String&gt; assmCode=new HashSet&lt;&gt;();
          assemblies.removeIf(e-&gt;!assmCode.add(e.getAssemblyCode())); //remove the last two abc1
  
          //but i need to add 4.2, 6.3 to 3.0 before removing
          System.out.println("List without duplicates: "+assemblies);
      }
  }
  
  class Assembly{
      private String assemblyCode;
      private double assemblyQty;
      //There will be more such fields
  
      public Assembly(String assemblyCode, double assemblyQty) {
          this.assemblyCode = assemblyCode;
          this.assemblyQty = assemblyQty;
      }
  
      public String getAssemblyCode() {
          return assemblyCode;
      }
  
      public void setAssemblyCode(String assemblyCode) {
          this.assemblyCode = assemblyCode;
      }
  
      public double getAssemblyQty() {
          return assemblyQty;
      }
  
      public void setAssemblyQty(double assemblyQty) {
          this.assemblyQty = assemblyQty;
      }
  
      @Override
      public String toString() {
          return "Assembly{" +
                  "assemblyCode='" + assemblyCode + '\'' +
                  ", assemblyQty=" + assemblyQty +
                  '}';
      }
  }
  </code></pre>
  
  <p>Please let me know if I can do an operation of such before removing the duplicate objects. I am new to java's collection api, so sorry for asking the question if the solution is easy.</p>
  
  <p>Just to clarify, the output must be like this:</p>
  
  <p>List with duplicates: [Assembly{assemblyCode='abc1', assemblyQty=3.0}, Assembly{assemblyCode='abc2', assemblyQty=6.0}, Assembly{assemblyCode='abc3', assemblyQty=8.4}, Assembly{assemblyCode='abc4', assemblyQty=9.0}, Assembly{assemblyCode='abc1', assemblyQty=4.2}, Assembly{assemblyCode='abc1', assemblyQty=6.3}]</p>
  
  <p>List without duplicates: [Assembly{assemblyCode='abc1', assemblyQty=13.5}, Assembly{assemblyCode='abc2', assemblyQty=6.0}, Assembly{assemblyCode='abc3', assemblyQty=8.4}, Assembly{assemblyCode='abc4', assemblyQty=9.0}]</p>