Compare two ArrayLists, remove matching items
Hello, I have two custom array lists, I want to remove similar items from one array list that match the second array list.
This is the logic I’m using.
List<Daily_Stock_Pojo> dailyStockArrayListOne = new ArrayList<Daily_Stock_Pojo>();
List<Daily_Stock_Pojo> dailyStockArrayListTwo = new ArrayList<Daily_Stock_Pojo>();
List<Daily_Stock_Pojo> added = new ArrayList<Daily_Stock_Pojo>(dailyStockArrayListOne);
added.removeAll(dailyStockArrayListTwo);
There is also my custom class, which is used as an object for an array list.
public class Daily_Stock_Pojo {
private Date Calendar_Date;
private int Store_Id;
private int Item_Id;
private int Stock_Volume;
private String MRP;
private String objectId;
public Daily_Stock_Pojo(Date calendar_Date, int store_Id, int item_Id, int stock_Volume, String MRP, String objectId) {
Calendar_Date = calendar_Date;
Store_Id = store_Id;
Item_Id = item_Id;
Stock_Volume = stock_Volume;
this. MRP = MRP;
this.objectId = objectId;
}
public Date getCalendar_Date() {
return Calendar_Date;
}
public void setCalendar_Date(Date calendar_Date) {
Calendar_Date = calendar_Date;
}
public int getStore_Id() {
return Store_Id;
}
public void setStore_Id(int store_Id) {
Store_Id = store_Id;
}
public int getItem_Id() {
return Item_Id;
}
public void setItem_Id(int item_Id) {
Item_Id = item_Id;
}
public int getStock_Volume() {
return Stock_Volume;
}
public void setStock_Volume(int stock_Volume) {
Stock_Volume = stock_Volume;
}
public String getMRP() {
return MRP;
}
public void setMRP(String MRP) {
this. MRP = MRP;
}
public String getObjectId() {
return objectId;
}
public void setObjectId(String objectId) {
this.objectId = objectId;
}
}
Give me a solution on how to compare two custom array lists and remove all items that match the second array list from the first array list.
Solution
Your algorithm is correct. You just need to correctly define the equals()
method for Daily_Stock_Pojo
. List#removeAll(
) will use equals()
to perform matching and remove elements.
If you do not define a suitable equals(),
you will use the definition in the Object class (all classes inherit from the Object
implicit class) and the implementation will only check the
references ( Object java doc for equals), which is usual Not what you wanted.
As a good measure, you should also define a hashCode()
method to override the method in Object
. This is useful, for example, if you intend to use an object as a key in a HashMap
or as an element in a HashSet
.
If you use an IDE like Eclipse, there should be an option to generate both methods. The idea is to use class properties that will determine how 2 objects are the same.
Edit
The following is the default implementation given by Eclipse when using the menu “Source>Generate hashCode() and equals()…” I choose to generate all the properties in the class. If you do not want the attribute to be part of the Daily_Stock_Pojo's
identity, remove it from the method.
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((Calendar_Date == null) ? 0 : Calendar_Date.hashCode());
result = prime * result + Item_Id;
result = prime * result + ((MRP == null) ? 0 : MRP.hashCode());
result = prime * result + Stock_Volume;
result = prime * result + Store_Id;
result = prime * result
+ ((objectId == null) ? 0 : objectId.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;
Daily_Stock_Pojo other = (Daily_Stock_Pojo) obj;
if (Calendar_Date == null) {
if (other. Calendar_Date != null)
return false;
} else if (! Calendar_Date.equals(other. Calendar_Date))
return false;
if (Item_Id != other. Item_Id)
return false;
if (MRP == null) {
if (other. MRP != null)
return false;
} else if (! MRP.equals(other. MRP))
return false;
if (Stock_Volume != other. Stock_Volume)
return false;
if (Store_Id != other. Store_Id)
return false;
if (objectId == null) {
if (other.objectId != null)
return false;
} else if (!objectId.equals(other.objectId))
return false;
return true;
}
Note:
You should apply the Java convention standard, i.e
Date Calendar_Date;
public Date getCalendar_Date() {
return Calendar_Date;
}
It should be:
Date calendarDate;
public Date getCalendarDate() {
return calendarDate;
}
Class members begin with a lowercase letter. There are no hyphens in class members or class names. Use CamelCase
(class name) or camelCase
(member name).