Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compare Two ArrayLists, Remove items that are matched

Hi I have two custom array lists, I want to remove the similar items from one array list that are matched with the second array lists.

Here is my logic that 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);

Also below one is my custom class used as an Object for the array lists.

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 the items from the first array list that are matched with the second array list.

like image 898
Srinivas69 Avatar asked Jan 26 '26 04:01

Srinivas69


1 Answers

Your algorithm is right. You just have to define properly the equals() method for Daily_Stock_Pojo. List#removeAll() will use the equals() to perform the match and remove the elements.

If you don't define a proper equals(), the one define in Object class is used (all classes inherit from Object class implicitly) and the implementation will only check references (Object java doc for equals), and that's generally not what you want.

As a good measure you should also define a hashCode() method that overrides the one from Object. This is useful for instance if you intend to use your 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 these two methods. The idea is to use the class attributes that will determine how 2 objects are identical.

EDIT Following is the default implementation given by Eclipse when using the menu "Source>Generate hashCode() and equals()...". I choose to generate on all attributes from the class. If you don't want an attribute to be part of the identity of Daily_Stock_Pojo, remove it from the methods.

   @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 Java convention standards, i.e.

Date Calendar_Date;

public Date getCalendar_Date() {
    return Calendar_Date;
}

Should be:

Date calendarDate;

public Date getCalendarDate() {
    return calendarDate;
}

Class members start with a lowercase. No hyphen in class members or class names. Use CamelCase (for a class name) or camelCase (for a member name).

like image 71
T.Gounelle Avatar answered Jan 27 '26 17:01

T.Gounelle