如何删除POJO类中等于某个值的所有元素?

时间:2018-04-21 10:32:59

标签: java android arraylist model pojo

我有一个像这样的Pojo课程:

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

//here all getter and setter 
}

以下是来自服务器的示例样本:

{
"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"  
}

我想要做的是删除上面示例中的所有元素id == 2,因此它会留下id==3,5 and 8元素。

我可以使用以下方法删除1个元素:

List<MyArrayList> myArrayList;

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

但是我不能一次性删除id==2的所有元素。有人请给我一个解决方案来做到这一点..

我的问题是,如何删除上面示例中id==2的所有4个元素?

由于

2 个答案:

答案 0 :(得分:0)

试试这个

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());
    }
}

活动代码

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() + "");
        }
    }


}

<强>输出

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

答案 1 :(得分:0)

List<MyArrayList> myArrayList;

//take new List
List<MyArrayList> newList = new Arraylist<>();

for(int i =0;i < myarraylist.size();i++){
        //id not matched
        if(myarraylist.get(i).getId() != 2){
            add to new list
            newList.add(i);

        }
    }