3

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.

Srinivas69
  • 43
  • 5
  • possible duplicate of [How to remove common values from two array list](http://stackoverflow.com/questions/15575417/how-to-remove-common-values-from-two-array-list) – Prudhvi Mar 14 '15 at 21:23
  • It seems to me that `removeAll()` should do the job for you. Can you please describe your problem or give an example of it? – madlymad Mar 14 '15 at 21:33

2 Answers2

3

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).

T.Gounelle
  • 5,953
  • 1
  • 22
  • 32
  • yes absolutely i need to create equals() and hashCode() methods. But home to write the code for above object class ? can you write code for equals() and hasCode() functions. – Srinivas69 Mar 15 '15 at 09:23
  • Yes you can! indeed, in Java, when you define a POJO or _bean_, i.e. an object that holds some data and provides _getters_ and, when needed, _setters_, you **must** redefine the `equals()` and `hashCode()` methods. This way you can use your object in collections or maps (for instance as a key in a `Map`). I will post what these method could be in my answer. – T.Gounelle Mar 15 '15 at 09:47
1

Here is an algorithm that you could follow to accomplish the task:

  • Construct a union of the two arrays
  • Construct the intersection of the two arrays
  • Subtract the intersection from the union to get your result

Referrence: Check this answer

Community
  • 1
  • 1
Prudhvi
  • 2,276
  • 7
  • 34
  • 54