Java 从列表中删除项目

Java 从列表中删除项目,java,collections,Java,Collections,我正在从学生的ArrayList中删除一个学生对象,这是我的代码,下面是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; privat

我正在从学生的ArrayList中删除一个学生对象,这是我的代码,下面是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;
}
}
这是我的课,有一个主要的方法

/*
 * 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();
    }
}
}
/*
*要更改此模板,请选择工具|模板
*然后在编辑器中打开模板。
*/
包corejava.list;
导入java.util.ArrayList;
导入java.util.Iterator;
导入java.util.List;
/**
* 
*@作者拉胡尔
*/
公共阶级革命者{
公共静态void main(字符串[]args){
List studentList=null;
试一试{
studentList=newarraylist(){{
增加(新学生(12名,“汤姆”);
增加(新学生(14名,“杰克”);
增加(新学生(15名,“朱利安”);
增加(新学生(16名,道格拉斯));
增加(新学生(17名,“芭丝谢芭”);
}};
for(迭代器itr=studentList.Iterator();itr.hasNext();){
System.out.println(itr.next());
}
System.out.println(studentList.remove(新学生(12));
for(迭代器itr=studentList.Iterator();itr.hasNext();){
System.out.println(itr.next());
}
}捕获(例外e){
e、 printStackTrace();
}
}
}
现在我的问题是,从ArrayList中删除一项是否安全,就像我在上面代码中所做的那样

hashCode()是否在从集合中删除Student对象中起作用

我们有更好的方法吗,你的问题

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

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

hashCode()是否在从集合中删除Student对象中起作用

不,
hashCode()
don在调用
Arraylist
remove函数时不起重要作用。如果查看,则会看到通过使用函数
equals
删除比较对象,因此函数
equals
在比较中起作用

hashCode()是否在从集合中删除Student对象中起作用

否。
equals()
方法起作用

例如,只需查看
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     }
>公共布尔删除(对象o){
440如果(o==null){
441 for(int index=0;index
像我在上面的代码中所做的那样,从数组列表中删除项是否安全


安全性取决于您对
Student
的相等定义,因为您要判断Id是否相等,那么Student是否相等,因此它是安全的(不谈论线程)。

由于您的
equals()
可以尝试强制转换非Student的对象,所以您实现的方式是不安全的。你应该:

@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()
在这种情况下不起作用。

您能否详细说明hashCode在删除中的作用?为什么要使用
try catch
块?
如果不需要(o==null)
大小写。将由
instanceof
处理。另外,最后5行可以替换为-
返回this.getId()==student.getId()
@RohitJain谢谢你的建议。顺便说一句,
o==this
虽然不是强制性的,但它是一种让事情变得更快的方法。这是一个小方法,但它可能更大,可以使用多次。我试图保留原来的方法,这就是为什么我没有使最后一行更好。我只是为了安全。