In this below example list having duplicate object of AccountTransaction which need to remove from list. Here I am using HashSet because it always keep unique records. Now question comes how to decide uniqueness of object. As you know contract between hashcode() and equals() method deciding uniqueness and equality of object.
Here used Comparable interface to sort values based on transaction date.
hashcode() and equals() contract :
“If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result. If you only override equals() and not hashCode() your class violates this contract.”
Example
import java.math.BigDecimal; import java.util.Date; public class AccountTransaction implements Comparable{ private Date date; String transactionType; private String reference; private BigDecimal amount; public AccountTransaction(Date date, String transactionType, String reference, BigDecimal amount) { super(); this.date = date; this.transactionType = transactionType; this.reference = reference; this.amount = amount; } //Overriding toString() method to print object @Override public String toString() { return "AccountTransactions [date=" + date + ", transactionType=" + transactionType + ", reference=" + reference + ", amount=" + amount + "]"; } //Overriding hashcode() and equals() method to check equality and uniqueness //of objects @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((amount == null) ? 0 : amount.hashCode()); result = prime * result + ((date == null) ? 0 : date.hashCode()); result = prime * result + ((reference == null) ? 0 : reference.hashCode()); result = prime * result + ((transactionType == null) ? 0 : transactionType.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; AccountTransaction other = (AccountTransaction) obj; if (amount == null) { if (other.amount != null) return false; } else if (!amount.equals(other.amount)) return false; if (date == null) { if (other.date != null) return false; } else if (!date.equals(other.date)) return false; if (reference == null) { if (other.reference != null) return false; } else if (!reference.equals(other.reference)) return false; if (transactionType == null) { if (other.transactionType != null) return false; } else if (!transactionType.equals(other.transactionType)) return false; return true; } //Sort object by date @Override public int compareTo(AccountTransaction o) { return this.getDate().compareTo(o.getDate()); } //use getter and setter of properties }
Here is the class having sample data which is having duplicate objects in list. calling removeDuplicate() method which is converting list to hashSet() to remove duplicate and then again converting to list then sorting by date.
import java.math.BigDecimal; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collections; import java.util.Date; import java.util.HashSet; import java.util.List; import java.util.Set; public class RemoveDuplicateObjects { public static void main(String[] args) { List transactionList = new ArrayList(); try { transactionList.add(new AccountTransaction(getDate("2018-10-15 10:50 AM"), "Account Debits", "Pizza hut",new BigDecimal("0.56"))); transactionList.add(new AccountTransaction(getDate("2018-10-15 10:52 AM"), "Account Debits", "Pizza hut",new BigDecimal("0.56"))); transactionList.add(new AccountTransaction(getDate("2018-10-15 10:48 AM"), "Account Debits", "Burger king",new BigDecimal("0.56"))); transactionList.add(new AccountTransaction(getDate("2018-10-15 10:38 AM"), "Account Debits", "Burger king",new BigDecimal("1.56"))); transactionList.add(new AccountTransaction(getDate("2018-10-15 10:55 AM"), "Account Debits", "Papa Johns",new BigDecimal("2.56"))); transactionList.add(new AccountTransaction(getDate("2018-10-15 10:35 AM"), "Account Debits", "Pizza hut",new BigDecimal("1.56"))); transactionList.add(new AccountTransaction(getDate("2018-10-15 10:35 AM"), "Account Credits", "Chase Bank",new BigDecimal("200"))); //Duplicate record transactionList.add(new AccountTransaction(getDate("2018-10-15 10:52 AM"), "Account Debits", "Pizza hut",new BigDecimal("0.56"))); transactionList.add(new AccountTransaction(getDate("2018-10-15 10:38 AM"), "Account Debits", "Burger king",new BigDecimal("1.56"))); transactionList.add(new AccountTransaction(getDate("2018-10-15 10:35 AM"), "Account Credits", "Chase Bank",new BigDecimal("200"))); System.out.println("Transactions before removing duplicate============="); for(AccountTransaction transaction:transactionList) System.out.println(transaction); System.out.println("Transactions after removing duplicate============="); transactionList=removeDuplicate(transactionList); for(AccountTransaction transaction:transactionList) System.out.println(transaction); } catch (Exception ex) { ex.printStackTrace(); } } private static List removeDuplicate(List transactionList) { //Convert List to Set Set transactionSet=new HashSet(transactionList); //Convert Set to Array List transactionList=new ArrayList(transactionSet); //Sort object by transaction date and time Collections.sort(transactionList); return transactionList; } private static Date getDate(String dateStr) throws ParseException { return new SimpleDateFormat("yyyy-MM-dd HH:mm a").parse(dateStr); } }
Output
Transactions before removing duplicate=============
AccountTransactions [date=Mon Oct 15 10:50:00 IST 2018, transactionType=Account Debits, reference=Pizza hut, amount=0.56]
AccountTransactions [date=Mon Oct 15 10:52:00 IST 2018, transactionType=Account Debits, reference=Pizza hut, amount=0.56]
AccountTransactions [date=Mon Oct 15 10:48:00 IST 2018, transactionType=Account Debits, reference=Burger king, amount=0.56]
AccountTransactions [date=Mon Oct 15 10:38:00 IST 2018, transactionType=Account Debits, reference=Burger king, amount=1.56]
AccountTransactions [date=Mon Oct 15 10:55:00 IST 2018, transactionType=Account Debits, reference=Papa Johns, amount=2.56]
AccountTransactions [date=Mon Oct 15 10:35:00 IST 2018, transactionType=Account Debits, reference=Pizza hut, amount=1.56]
AccountTransactions [date=Mon Oct 15 10:35:00 IST 2018, transactionType=Account Credits, reference=Chase Bank, amount=200]
AccountTransactions [date=Mon Oct 15 10:52:00 IST 2018, transactionType=Account Debits, reference=Pizza hut, amount=0.56]
AccountTransactions [date=Mon Oct 15 10:38:00 IST 2018, transactionType=Account Debits, reference=Burger king, amount=1.56]
AccountTransactions [date=Mon Oct 15 10:35:00 IST 2018, transactionType=Account Credits, reference=Chase Bank, amount=200]
Transactions after removing duplicate=============
AccountTransactions [date=Mon Oct 15 10:35:00 IST 2018, transactionType=Account Credits, reference=Chase Bank, amount=200]
AccountTransactions [date=Mon Oct 15 10:35:00 IST 2018, transactionType=Account Debits, reference=Pizza hut, amount=1.56]
AccountTransactions [date=Mon Oct 15 10:38:00 IST 2018, transactionType=Account Debits, reference=Burger king, amount=1.56]
AccountTransactions [date=Mon Oct 15 10:48:00 IST 2018, transactionType=Account Debits, reference=Burger king, amount=0.56]
AccountTransactions [date=Mon Oct 15 10:50:00 IST 2018, transactionType=Account Debits, reference=Pizza hut, amount=0.56]
AccountTransactions [date=Mon Oct 15 10:52:00 IST 2018, transactionType=Account Debits, reference=Pizza hut, amount=0.56]
AccountTransactions [date=Mon Oct 15 10:55:00 IST 2018, transactionType=Account Debits, reference=Papa Johns, amount=2.56]
See Also:
- Java : Sort Date Object and Text Format
- Java : Comparable Vs Comparator
- How to sort object by Comparator interface in ascending and descending order : JAVA
- How to Sort By Comparable Interface in Ascending and Descending Order : Java
- Sort ArrayList in Ascending or Descending Order or Natural or Chronological Order
You must log in to post a comment.