0

I have this list which is collection of GrossBalanceDtlsVO class.

GrossBalanceDtlsVO class contains below field :

private Long customerID = null;

private BigDecimal instAmount = BigDecimal.ZERO;

private BigDecimal instBalanceAmount = BigDecimal.ZERO;

private BigDecimal instRedemptionAmt = BigDecimal.ZERO; 

private Long instTxnCnt = null;

private Date monthEndDt = null;
....
....



List<GrossBalanceDtlsVO> grossDetailsVO2 = getPCCustomerDetails(customerID);

This list contains duplicate data. For example :

customerID=1000033195, monthEndDt=2016-05-31, recMonthNo=9, recMonthlyLimit=100, recBalanceAmt=100, recMonthlyRedemption=0, recRollOverEligibility=0, txnId=null, instTxnCnt=null, instAmount=0, instBalanceAmount=0, instRedemptionAmt=0]
customerID=1000033195, monthEndDt=2016-06-30, recMonthNo=10, recMonthlyLimit=100, recBalanceAmt=100, recMonthlyRedemption=0, recRollOverEligibility=0, txnId=null, instTxnCnt=null, instAmount=0, instBalanceAmount=0, instRedemptionAmt=0]
customerID=1000033195, monthEndDt=2016-07-31, recMonthNo=11, recMonthlyLimit=100, recBalanceAmt=100, recMonthlyRedemption=0, recRollOverEligibility=0, txnId=null, instTxnCnt=null, instAmount=0, instBalanceAmount=0, instRedemptionAmt=0]
customerID=1000033195, monthEndDt=2016-05-31, recMonthNo=9, recMonthlyLimit=100, recBalanceAmt=100, recMonthlyRedemption=0, recRollOverEligibility=0, txnId=null, instTxnCnt=null, instAmount=0, instBalanceAmount=0, instRedemptionAmt=0]
customerID=1000033195, monthEndDt=2016-06-30, recMonthNo=10, recMonthlyLimit=100, recBalanceAmt=100, recMonthlyRedemption=0, recRollOverEligibility=0, txnId=null, instTxnCnt=null, instAmount=0, instBalanceAmount=0, instRedemptionAmt=0]
customerID=1000033195, monthEndDt=2016-07-31, recMonthNo=11, recMonthlyLimit=100, recBalanceAmt=100, recMonthlyRedemption=0, recRollOverEligibility=0, txnId=null, instTxnCnt=null, instAmount=0, instBalanceAmount=0, instRedemptionAmt=0]

Now my question is that how to remove those duplicate data from this list?

Shiladittya Chakraborty
  • 4,270
  • 8
  • 45
  • 94
  • 2
    Do your objects have `equals` and `hashcode` methods? Do you need to preserve original order? – Tamas Rev May 19 '16 at 10:50
  • Is customer id the unique identifier? If yes how are other parameters different with same cust id? – Amriteya May 19 '16 at 10:51
  • iterate, create the unique key from the object, put in a hashmap and when done use the hashmap values? –  May 19 '16 at 10:53
  • redefine equal an hashcode methods in GrossBalanceDtlsVO and use Set List to remove duplication. – JHDev May 19 '16 at 10:54
  • I don't have the equals and hashcode. Even how can I use those methods to remove duplicate? order is not necessity @tamasrev – Shiladittya Chakraborty May 19 '16 at 10:58
  • @Shiladittya Chakraborty covert your list into set and set autometically removes duplicate elements – Dhiraj May 19 '16 at 11:02
  • @ShiladittyaChakraborty it will help you i recentely tried with custom object. – Dhiraj May 19 '16 at 11:04
  • If you have good `equals()` and `hashcode()` then you can filter duplicates with a `HashSet`. Anyway, you need to determine whether these `GrossBalanceDtlsVO ` equal. Then, why not add an `equals` method? – Tamas Rev May 19 '16 at 11:43

2 Answers2

1

Sort them by given criteria, iterate them and check if the previous element is equal to the current element, if true - push them in separate list/array, and after that iterate this list/array and remove the items from your list.

Note: you should write your own object comparator.

For future use, if you want to have distinct collection - use a set.

Joeri Hendrickx
  • 16,947
  • 4
  • 41
  • 53
Vaptsarov
  • 86
  • 9
0

You could throw your list into a Set and this Set into a new List.

List<GrossBalanceDtlsVO> grossDetailsVO2 = getPCCustomerDetails(customerID);
Set<GrossBalanceDtlsVO> grossDetailsVO2Set = new HashSet<>(grossDetailsVO2);
List<GrossBalanceDtlsVO> grossDetailsVO2WithoutDuplicates = new ArrayList<>(grossDetailsVO2Set);
Frank
  • 2,036
  • 1
  • 20
  • 32
  • This way does only work if you have equals() and hashCode() implemented. So you should implement those two methods (perhaps using `EqualsBuilder` and `HashCodeBuilder` or with Guavas `Objects` as stated in the answer to [this SO-question](http://stackoverflow.com/questions/5038204/apache-commons-equals-hashcode-builder)) – Frank May 19 '16 at 11:08
  • keep in mind that using a set would mean you lose order. – Joeri Hendrickx May 19 '16 at 14:36