1

How can I get duplicates from an ArrayList?

public class Order {

    private String portId;
    private String action;
    private String idType;
    private String id;
    private BigDecimal amount;


    public String getPortId() {
        return portId;
    }
    public void setPortId(String portId) {
        this.portId = portId;
    }
    public String getAction() {
        return action;
    }
    public void setAction(String action) {
        this.action = action;
    }
    public String getIdType() {
        return idType;
    }
    public void setIdType(String idType) {
        this.idType = idType;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public BigDecimal getAmount() {
        return amount;
    }
    public void setAmount(BigDecimal amount) {
        this.amount = amount;
    }


}

My code:

List<Order> duplicateList = new ArrayList<Order>();
List<Order> nonDuplicateList = new ArrayList<Order>();

Set<Order> set = new HashSet<Order>();
for (Order order : listContainingAllOrders) {
    if (!set.add(order)) {

        duplicateList.add(order);
    } else {
        nonDuplicateList.add(order);
    }
}

I want to achieve duplicateList and nonDuplicateList, where I will combine both the duplicate list and Non duplicate List together and display on the UI. The duplicate Orders will be Identified by Error Message column.

5
  • You should think about what your functions are doing. Stepping through your program with a debugger can be very helpful. Though I'd probably take the easier route and just print stuff. Either way, understand your flow. Commented Aug 12, 2014 at 20:40
  • 3
    Does your Order class properly implement hashCode and equals? If it doesn't, then you'll never have the "add" method returning true unless they are exactly the same object. Commented Aug 12, 2014 at 20:48
  • How to override HashCode and equals methods I have 5 variables in Order Object Commented Aug 12, 2014 at 21:44
  • @user3810342 Please post your Order class. Commented Aug 13, 2014 at 1:52
  • @user3810342 Hey! I added my Class. Please let me know if you need any Info. I really appreciate your help in solving this problem. Commented Aug 13, 2014 at 2:12

1 Answer 1

4

If I understand you, then you could replace this

if (!set1.add(order)) {

with

if (!set1.contains(order)) {
  set1.add(order);

Edit

You need to Override equals() and hashCode() in your Order. Assuming that Order(s) with the same id are equal by definition - one possible way would be,

@Override
public boolean equals(Object obj) {
  if (obj instanceof Order) {
    if (id == null) {
      // return ((Order) obj).id == null;
      return false; // probably best to prevent null
    }
    return id.equals(((Order) obj).id);
  }
  return false;
}

@Override
public int hashCode() {
  return id.hashCode();
}
Sign up to request clarification or add additional context in comments.

1 Comment

This is strange. The code above should work as expected unless the repeated elements are the same object references.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.