使用特定值从ArrayList中删除项目

时间:2015-03-17 18:52:30

标签: java arraylist

我创建了一个对象列表并添加了人员:

ArrayList<Person> peeps = new ArrayList<Person>(); 

peeps.add(new Person("112", "John", "Smith"));
peeps.add(new Person("516", "Jane", "Smith"));
peeps.add(new Person("114", "John", "Doe"));

我正在尝试弄清楚如何通过ID号从列表中删除此人。因此,如果我想删除身份证号码为114的人,但现在没有列入名单的人,我该怎么办?

7 个答案:

答案 0 :(得分:36)

使用Java8:

peeps.removeIf(p -> p.getId().equals("112"));

请注意,这相当于线性搜索,并且需要O(n)次。如果经常重复此操作,建议使用HashMap以加快O(1)的速度。

或者使用排序列表也可以做到这一点,但需要O(log n)时间。

答案 1 :(得分:13)

如果您要使用ArrayList,唯一的方法是遍历整个列表,查看每个人,并查看它们的ID号为114.对于较大的数据集,这样做不会有效应该避免。

如果您可以更改数据结构,那么某种Map会更好(HashMap通常是一个不错的选择)。您可以将身份证号码作为“密钥”,然后将其与每个人关联。稍后您可以按键查询Map。 con是你只能有一个值作为键,所以你不能说出姓名和身份证号码

编辑:
使用ArrayList的一种更有效的方法是保持按ID号排序。然后,您可以使用Collections.binarySearch()之类的内容快速按ID号访问元素。结果是从/插入到已排序的数组中是昂贵的,因为必须移动更大的元素。因此,如果与读取次数相比,您将进行相对较少的更改,这可能是可行的

答案 2 :(得分:5)

有很多方法可以解决这个问题。

  1. 我最喜欢的是使用apache.common.collection4或其谷歌等效的CollectionUtils。然后使用谓词或java 8中的lambda表达式选择您想要的内容。

    CollectionUtils.select(peeps, new Predicate<Person>() { @Override public boolean evaluate(Person object) { return object.getId().equals("114"); } });

  2. 使用旧的迭代器并循环遍历

    Iterator<Person> iterator = peeps.iterator(); while(iterator.hasNext()) { Person next = iterator.next(); if(next.getId().equals("114")) { iterator.remove(); } }

答案 3 :(得分:3)

iterate元素中的

ArrayList删除与您要删除的字符串匹配的元素: Iterator remove操作是安全的,不会创建ConcurrentModificationException

for (Iterator<String> iterator = peeps.iterator(); elementToCheck = iterator.next();) {
    if (elementToCheck.getId().equals("112")) {
        // Remove the current element from the iterator and the list.
        iterator.remove();
    }
}

答案 4 :(得分:2)

首先需要在Person类中使用equals(您应该这样做)。然后,您可以使用List#indexOfList#remove。例如:

final Person toRemove = new Person("112");
peeps.remove(peeps.indexOf(toRemove));

(假设人员ID是唯一的)。

或者,如果您的列表是ArrayList,则可以使用ArrayList#remove(Object)

final Person toRemove = new Person("112");
peeps.remove(toRemove);

如果您使用的是Java 8,则可以使用Paul's solution

答案 5 :(得分:0)

如果你想搜索字符串那么应该使用 .equals

String query;
ArrayList<String> list;

   for(int i=0; i < list.size();i++)
       if (list.get(i).equals(query)){
           list.remove(i);
           break;
       }

答案 6 :(得分:-1)

class Processor{

ArrayList<Person> peeps = new ArrayList<Person>(); 

void setPeeps(){
    peeps.add(new Person(112, "John", "Smith"));
    peeps.add(new Person(516, "Jane", "Smith"));
    peeps.add(new Person(114, "John", "Doe"));
}

void removePerson(int id){
    for(int i=0; i <= peeps.size(); i++){
        Person person = peeps.get(i);
        if(person.id == id)
            peeps.remove(peeps.get(i));
    }
}

void displayPersonsList(){
    for(Person person : peeps){
        System.out.println(person.id + ":" + person.fName + ":" + person.lName);
    }
}

public static void main(String args[]){
    Processor objProcessor = new Processor();
    objProcessor.setPeeps();
    objProcessor.removePerson(114);
    objProcessor.displayPersonsList();
}
}

class Person{
int id;
String fName;
String lName;

public Person(int id, String fName, String lName){
    this.id = id;
    this.fName = fName;
    this.lName = lName;
}
}
相关问题