Search code examples
javaandroidarraylistmodelpojo

How to remove all the element which equal to certain value in POJO classes?


I have a Pojo class like this:

public class Item
{
  private int id;
  private String username;

//here all getter and setter 
}

Here is the sample of my sample from server:

{
"id" : 2
"name" : "Ali"

"id" : 3
"name" : "janice"

"id" : 2
"name" : "Ali"

"id" : 5
"name" : "tupac"

"id" : 2
"name" : "Ali"

"id" : 8
"name" : "William"

"id" : 2
"name" : "Ali"  
}

What I want to do is remove all the element inside the sample above,which the id == 2,so it will left element with id==3,5 and 8.

I can remove 1 element with the method below:

List<MyArrayList> myArrayList;

for(int i =0;i < myarraylist.size();i++){
        if(myarraylist.get(i).getId() == 2){
            myarraylist.remove(i);
        }
    }

But I cant remove all the element which id==2 in one time.Somebody please give me a solution to do this..

My question is,how can I remove all 4 element which is id==2 in the sample above?

Thanks


Solution

  • Try this

    POJO CLASS

        public class Item {
    
        private int id;
        private String username;
    
    
        public Item(int id, String username) {
            this.id = id;
            this.username = username;
        }
    
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
    
        @Override
        public boolean equals(Object obj) {
            // TODO Auto-generated method stub
            if (obj instanceof Item) {
                Item temp = (Item) obj;
                if (this.id == temp.id)
                    return true;
            }
            return false;
    
        }
    
        @Override
        public int hashCode() {
            // TODO Auto-generated method stub
    
            return (this.id.hashCode() + this.username.hashCode());
        }
    }
    

    ACTIVITY CODE

    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    import java.util.ArrayList;
    import java.util.HashSet;
    import java.util.Set;
    
    
    public class MainActivity extends AppCompatActivity {
    
    
        ArrayList<Item> arrayList = new ArrayList<>();
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            arrayList.clear();
    
            Item item = new Item(1, "UserName 1");
    
            arrayList.add(item);
    
            for (int i = 0; i < 3; i++) {
                Item item2 = new Item(2, "UserName 2");
                arrayList.add(item2);
    
            }
    
            Item item3 = new Item(3, "UserName 3");
            arrayList.add(item3);
    
    
            Item item4 = new Item(4, "UserName4");
            arrayList.add(item4);
    
            Item item5 = new Item(5, "UserName 5");
            arrayList.add(item5);
    
    
            Set<Item> s = new HashSet<Item>();
            s.addAll(arrayList);
            arrayList = new ArrayList<Item>();
            arrayList.addAll(s);
    
            for (int i=0;i<arrayList.size();i++){
                if (arrayList.get(i).getId() == 2) {
                    arrayList.remove(i);
                }
            }
    
            for (int i = 0; i < arrayList.size(); i++) {
                Log.e("ID      :", arrayList.get(i).getId() + "");
                Log.e("UserName:", arrayList.get(i).getUsername() + "");
            }
        }
    
    
    }
    

    OUTPUT

    04-21 16:56:40.114 com.example.nilesh.testapp E/ID      :: 1
    04-21 16:56:40.114 com.example.nilesh.testapp E/UserName:: UserName 1
    04-21 16:56:40.114 com.example.nilesh.testapp E/ID      :: 5
    04-21 16:56:40.114 com.example.nilesh.testapp E/UserName:: UserName 5
    04-21 16:56:40.114 com.example.nilesh.testapp E/ID      :: 4
    04-21 16:56:40.114 com.example.nilesh.testapp E/UserName:: UserName4
    04-21 16:56:40.114 com.example.nilesh.testapp E/ID      :: 3
    04-21 16:56:40.114 com.example.nilesh.testapp E/UserName:: UserName 3