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.
Orderclass properly implementhashCodeandequals? If it doesn't, then you'll never have the "add" method returning true unless they are exactly the same object.Orderclass.