从列表中删除项目

时间:2013-12-03 13:33:50

标签: java collections

我从学生的ArrayList中删除一个Student对象,这是我的代码,下面是Student.java

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package corejava.list;

/**
 * 
 * @author Rahul 
 */

public class Student {
private int id;
private String name;

public Student(int id,String name){
    this.id = id;
    this.name = name;
}

public Student(int id){
    this.id = id;        
}

@Override
public int hashCode(){
    return this.getId() * 37;
}

@Override
public String toString(){
    StringBuffer strb = new StringBuffer();
    strb.append("\tID : ").append(this.getId()).append(", NAME : ").append(this.getName());
    return strb.toString();
}

@Override
public boolean equals(Object studentOne){
    Student student = (Student) studentOne;
    boolean flag = false;
    if(this.getId() == student.getId()){
        flag = true;
    }
    return flag;
}

/**
 * @return the id
 */
public int getId() {
    return id;
}

/**
 * @return the name
 */
public String getName() {
    return name;
}
}

这是我的Class,它有一个主方法,

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package corejava.list;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 
 * @author Rahul
 */



public class RemoveList {
    public static void main(String [] args){
        List<Student> studentList = null;
        try{
        studentList = new ArrayList<Student>(){{
            add(new Student(12,"Tom"));
            add(new Student(14, "Jack"));
            add(new Student(15, "Julean"));
            add(new Student(16, "Doughlas"));                
            add(new Student(17, "Bathsheba"));
        }};

        for(Iterator<Student> itr = studentList.iterator(); itr.hasNext();){
            System.out.println(itr.next());
        }

        System.out.println(studentList.remove(new Student(12)));

        for(Iterator<Student> itr = studentList.iterator(); itr.hasNext();){
            System.out.println(itr.next());
        }

    }catch(Exception e){
        e.printStackTrace();
    }
}
}

现在我的问题是,可以安全地从ArrayList中移除一个项目,就像我在上面的代码中所做的那样,

hashCode()在从Collection中删除Student对象时起作用,

我们有更好的方法,

3 个答案:

答案 0 :(得分:5)

  

hashCode()在从Collection中删除Student对象时起作用,

没有。 equals()方法发挥作用。

例如,只需查看source code of ArrayList remove()方法

即可
>public boolean  remove(Object o) {
440         if (o == null) {
441             for (int index = 0; index < size; index++)
442                 if (elementData[index] == null) {
443                     fastRemove(index);
444                     return true;
445                 }
446         } else {
447             for (int index = 0; index < size; index++)
448                 if (o.equals(elementData[index])) {  //here
449                     fastRemove(index);
450                     return true;
451                 }
452         }
453         return false;
454     }
  

如上所述,我可以安全地从数组列表中删除项目。

safe 取决于Student Equal 定义,因为你告诉Id是否相等,那么Student等于因此它是安全的(不是在谈论线程)。

答案 1 :(得分:2)

由于您的equals()可以尝试投射不是学生的对象,因此您实施的方式并不安全。你应该对待它:

@Override
public boolean equals(Object o){
    //-- add it to your code --//
    if(o == this) 
        return true;

    if(!(o instanceof Student)) 
        return false;

    //-- at this point you're safe to cast --//

    Student student = (Student) o;
    boolean flag = false;
    if(this.getId() == student.getId()){
        flag = true;
    }
    return flag;
}

hashCode()在这种情况下不起作用。

答案 2 :(得分:1)

你的问题

  

是,可以安全地从ArrayList中删除一个项目,就像我在上面的代码中所做的那样,

是的,它是安全的(我认为安全是线程安全的,或者通过提供object作为参数来使用是正确的)因为当调用remove函数时没有其他线程正在执行任何其他操作。

  

hashCode()在从Collection中删除Student对象时起作用,

不,hashCode()在您调用Arraylist删除功能时不起重要作用。如果查看source code for remove function,则会看到使用函数equals删除比较对象,因此覆盖函数equals会在比较中起作用。