title: how to filter duplicate ArrayList by id in Java - spring boot data jpa
body: <p>In my test application i am trying to create a springboot api. I have create one api through which i am getting a list of object which contain duplicate id(sid). Now i want to filter that list by sid and merge the values (custRefId) with the corresponding ids.</p>
  
  <p><strong><em>List Output from Api :</em></strong></p>
  
  <pre><code>[
      {
          "sId": 101,
          "sName": "Nike",
          "custRefId": "S1234567890a1001INR"
      },
      {
          "sId": 201,
          "sName": "Addidas",
          "custRefId": "S1234567895a1004INR"
      },
      {
          "sId": 501,
          "sName": "U.S Polo",
          "custRefId": "S1234567000a1258INR"
      },
      {
          "sId": 501,
          "sName": "U.S Polo",
          "custRefId": "S1234567000a1011INR"
      },
      {
          "sId": 501,
          "sName": "U.S Polo",
          "custRefId": "S1234567899a1008INR"
      }
  ]
  </code></pre>
  
  <p><strong><em>Required OutPut :</em></strong></p>
  
  <pre><code>{
          "sId": 101,
          "sName": "Nike",
          "custRefId": "S1234567890a1001INR"
      },
      {
          "sId": 201,
          "sName": "Addidas",
          "custRefId": "S1234567895a1004INR"
      },
      {
          "sId": 501,
          "sName": "U.S Polo",
          "custRefId": {"S1234567000a1258INR","S1234567000a1011INR","S1234567899a1008INR"}
      }
  </code></pre>
  
  <hr>
  
  <pre><code>enter code here
  </code></pre>
  
  <p><strong>My test bean</strong></p>
  
  <pre><code>package com.example.easynotes.model;
  
  import java.io.Serializable;
  import java.util.Date;
  
  import javax.persistence.Column;
  import javax.persistence.EmbeddedId;
  import javax.persistence.Entity;
  import javax.persistence.GeneratedValue;
  import javax.persistence.GenerationType;
  import javax.persistence.Id;
  import javax.persistence.Table;
  
  import com.example.easynotes.controller.CompositeKeyEntity;
  
  @Entity(name = "TestApi")
  @Table(name="test")
  public class TestApi implements Serializable{
  
      /**
       * 
       */
      private static final long serialVersionUID = -6972984895555407355L;
  
      @EmbeddedId
      private CompositeKeyEntity CompositeKeyEntity;
  
      @Column(name = "S_Id")
      private Long sId;
  
      @Column(name = "S_Name")
      private String sName;
  
      @Column(name = "B_Id")
      private int bId;
  
      @Column(name = "B_Name")
      private String bName;
  
      @Column(name = "Login_Date")
      private Date loginDate;
  
      @Column(name = "Amount")
      private int amount;
  
      @Column(name = "B_Acc")
      private String bAccount;
  
      public TestApi() {
          super();
      }
  
      public TestApi(com.example.easynotes.controller.CompositeKeyEntity compositeKeyEntity, Long sId, String sName,
              int bId, String bName, Date loginDate, int amount, String bAccount) {
          super();
          CompositeKeyEntity = compositeKeyEntity;
          this.sId = sId;
          this.sName = sName;
          this.bId = bId;
          this.bName = bName;
          this.loginDate = loginDate;
          this.amount = amount;
          this.bAccount = bAccount;
      }
  
      public CompositeKeyEntity getCompositeKeyEntity() {
          return CompositeKeyEntity;
      }
  
      public void setCompositeKeyEntity(CompositeKeyEntity compositeKeyEntity) {
          CompositeKeyEntity = compositeKeyEntity;
      }
  
      public Long getsId() {
          return sId;
      }
  
      public void setsId(Long sId) {
          this.sId = sId;
      }
  
      public String getsName() {
          return sName;
      }
  
      public void setsName(String sName) {
          this.sName = sName;
      }
  
      public int getbId() {
          return bId;
      }
  
      public void setbId(int bId) {
          this.bId = bId;
      }
  
      public String getbName() {
          return bName;
      }
  
      public void setbName(String bName) {
          this.bName = bName;
      }
  
      public Date getLoginDate() {
          return loginDate;
      }
  
      public void setLoginDate(Date loginDate) {
          this.loginDate = loginDate;
      }
  
      public int getAmount() {
          return amount;
      }
  
      public void setAmount(int amount) {
          this.amount = amount;
      }
  
      public String getbAccount() {
          return bAccount;
      }
  
      public void setbAccount(String bAccount) {
          this.bAccount = bAccount;
      }
  
  }
  </code></pre>
  
  <p><strong>Test Api Repository :</strong></p>
  
  <pre><code>package com.example.easynotes.repository;
  import java.util.List;
  
  import org.springframework.data.jpa.repository.JpaRepository;
  import org.springframework.data.jpa.repository.Query;
  import org.springframework.data.repository.query.Param;
  import org.springframework.stereotype.Repository;
  
  import com.example.easynotes.controller.CompositeKeyEntity;
  import com.example.easynotes.model.TestApi;
  
  @Repository
  public interface TestRepo extends JpaRepository&lt;TestApi, CompositeKeyEntity&gt;{
  
      @Query(
              value = "SELECT * FROM test WHERE S_Id = :sId",
              nativeQuery = true
      )
      public TestApi getSDataById(@Param("sId")long sId);
  
      @Query(value="SELECT h.S_Id,h.S_Name,h.B_Id,h.B_Name,h.Login_Date,h.Amount,h.B_Acc,h.S_Acc,h.B_Ref_Id,h.CRR  FROM test h order by h.Login_Date asc",nativeQuery = true )
      public List&lt;TestApi&gt; getSellerData();
  
  
      @Query(
              value = "SELECT * FROM test WHERE S_Id = :sId and S_Name =:sName",
              nativeQuery = true
      )
      public List&lt;TestApi&gt; findDataBySIDAndSName(long sId, String sName);
  
  }
  </code></pre>
  
  <p><strong><em>TestController Class :</em></strong></p>
  
  <pre><code>package com.example.easynotes.controller;
  
  import java.util.ArrayList;
  import java.util.Collection;
  import java.util.List;
  import java.util.Map;
  import java.util.Set;
  import java.util.stream.Stream;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.web.bind.annotation.GetMapping;
  import org.springframework.web.bind.annotation.PathVariable;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RestController;
  import com.example.easynotes.model.TestApi;
  import com.example.easynotes.model.TestBean;
  import com.example.easynotes.repository.TestRepo;
  import com.google.common.collect.ArrayListMultimap;
  import com.google.common.collect.Multimap;
  
  @RestController
  @RequestMapping("/LCRF")
  public class TestController {
  
  
      List&lt;TestApi&gt; lcrf = new ArrayList&lt;TestApi&gt;();
      List&lt;TestApi&gt; tmp = new ArrayList&lt;TestApi&gt;();
  
      @Autowired
      TestRepo lRepo;
      @Autowired
      Assembler assembler;
  
      @GetMapping("/allData")
      public List&lt;TestBean&gt; getAll()
      {
          getMergeIds(assembler.sBBeanList(lRepo.getSellerData()));
  
          return assembler.sBBeanList(lRepo.getSellerData());
      }
  
      @GetMapping("/allData/{sellerId}")
      public TestApi getSellerDetail(@PathVariable(value = "sellerId")Long sellerId)
      {
  
          return lRepo.getSDataById(sellerId);
      }
  
      @GetMapping("/allData/{sId}/{sName}")
      public List&lt;TestApi&gt; getDataByIdAndName(@PathVariable(value = "sId")Long sId,@PathVariable(value = "sId")String sName)
      {
  
          lcrf = lRepo.findDataBySIDAndSName(sId, sName);
          return lcrf;    
      }
  
      public List&lt;TestMergeIds&gt; getMergeIds(List&lt;TestBean&gt; testBean)
      {
          List&lt;TestBean&gt; testList = new ArrayList&lt;TestBean&gt;();
          Multimap&lt;Long, String&gt; multiMap = ArrayListMultimap.create();
          TestMergeIds testMergeIdsBean = new TestMergeIds(); 
          for(TestBean bean: testBean) {
  
              multiMap.put(bean.getsId(), bean.getCustRefId());
  
          }
          Set&lt;Long&gt; keys = multiMap.keySet();
          List&lt;String&gt; targetList = new ArrayList&lt;&gt;();
          for (Long key : keys) {
  
          }
          System.out.println("Print list values ---111---"+targetList);
          return null;
  
  
      }
  }
  </code></pre>
  
  <p><strong><em>CompositeKeyEntity Class :</em></strong></p>
  
  <pre><code>package com.example.easynotes.controller;
  
  import java.io.Serializable;
  
  import javax.persistence.Column;
  import javax.persistence.Embeddable;
  
  @Embeddable
  public class CompositeKeyEntity implements Serializable{
  
      private static final long serialVersionUID = 1581433221916043048L;
  
      @Column(name="S_Acc")
      private String sAccount;
  
      @Column(name="B_Ref_Id")
      private String bRefId;
  
      @Column(name="CRR")
      private String crr;
  
      public String getsAccount() {
          return sAccount;
      }
  
      public void setsAccount(String sAccount) {
          this.sAccount = sAccount;
      }
  
      public String getbRefId() {
          return bRefId;
      }
  
      public void setbRefId(String bRefId) {
          this.bRefId = bRefId;
      }
  
      public String getCrr() {
          return crr;
      }
  
      public void setCrr(String crr) {
          this.crr = crr;
      }
  
  
  
  
  
  }
  </code></pre>
  
  <p><strong>Assembler Class :</strong></p>
  
  <pre><code>package com.example.easynotes.controller;
  
  import java.util.ArrayList;
  import java.util.Collection;
  import java.util.Iterator;
  import java.util.List;
  import java.util.ListIterator;
  
  import org.springframework.beans.BeanUtils;
  import org.springframework.stereotype.Component;
  
  import com.example.easynotes.model.TestApi;
  import com.example.easynotes.model.TestBean;
  
  @Component
  public class Assembler {
  
      public List&lt;TestBean&gt; sBBeanList(final List&lt;TestApi&gt; testApiList)
      {
          final List&lt;TestBean&gt; SellerBuyerBeanList = new ArrayList&lt;&gt;(testApiList.size());
          if(!testApiList.isEmpty())
          {
              for(final TestApi lcrfApiEntity : testApiList) {
  
                  final TestBean sellerBuyerBeanlist = new TestBean();
                  BeanUtils.copyProperties(lcrfApiEntity, sellerBuyerBeanlist);
                  sellerBuyerBeanlist.setCustRefId(formSellerBuyerRefId(lcrfApiEntity.getCompositeKeyEntity()));
                  SellerBuyerBeanList.add(sellerBuyerBeanlist);
              }
  
          }
  
  
          return SellerBuyerBeanList;
  
      }
  
      private String formSellerBuyerRefId(final CompositeKeyEntity compositeKeyEntity)
      {
          StringBuilder sBuilder=new StringBuilder();
          sBuilder.append(compositeKeyEntity.getsAccount());
          sBuilder.append(compositeKeyEntity.getbRefId());
          sBuilder.append(compositeKeyEntity.getCrr());
          return sBuilder.toString();
  
      }
  }
  enter code here
  </code></pre>