Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 当“仅等于”被覆盖时,为什么列表会将项查找为true?_Java_Hashmap_Hashtable_Equals_Hashcode - Fatal编程技术网

Java 当“仅等于”被覆盖时,为什么列表会将项查找为true?

Java 当“仅等于”被覆盖时,为什么列表会将项查找为true?,java,hashmap,hashtable,equals,hashcode,Java,Hashmap,Hashtable,Equals,Hashcode,我知道hashcode和equals都需要重写。我尝试了下面的程序,想知道为什么列表能够搜索学生,而set不能搜索学生,因为只有equals被覆盖: import java.util.*; class Student { private int id; private String name; public Student(int id, String name) { this.name = name; this.id = id; } public int getId()

我知道hashcode和equals都需要重写。我尝试了下面的程序,想知道为什么列表能够搜索学生,而set不能搜索学生,因为只有equals被覆盖:

import java.util.*;

class Student {
private int id;
private String name;

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

public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

@Override
public boolean equals(Object obj) {
    if (obj == null) return false;

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

    if (obj == this)
        return true;

    return this.getId() == ((Student) obj).getId();
}
}

public class StudentHashcodeEquals {
public static void main(String[] args) {
    Student alex1 = new Student(1, "Alex");
    Student alex2 = new Student(1, "Alex");

    System.out.println("alex1 hashcode = " + alex1.hashCode());
    System.out.println("alex2 hashcode = " + alex2.hashCode());
    System.out.println("Checking equality between alex1 and alex2 = " + alex1.equals(alex2));

    List<Student> studentsLst = new ArrayList <Student>();
    studentsLst.add(alex1);
    studentsLst.add(alex2);

    System.out.println("Arraylist size = " + studentsLst.size());
    System.out.println("Arraylist contains Alex = " + studentsLst.contains(new Student(1, "Alex")));

    HashSet <Student> students = new HashSet <Student>();
    students.add(alex1);
    students.add(alex2);

    System.out.println("HashSet size = " + students.size());
    System.out.println("HashSet contains Alex = " + students.contains(new Student(1, "Alex")));
}
}

/*
alex1 hashcode = 366712642
alex2 hashcode = 1829164700
Checking equality between alex1 and alex2 = true
Arraylist size = 2
Arraylist contains Alex = true
HashSet size = 2
HashSet contains Alex = false
*/
import java.util.*;
班级学生{
私有int-id;
私有字符串名称;
公立学生(整数id,字符串名称){
this.name=名称;
this.id=id;
}
公共int getId(){
返回id;
}
公共无效集合id(内部id){
this.id=id;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
@凌驾
公共布尔等于(对象obj){
if(obj==null)返回false;
如果(!(学生的obj实例))
返回false;
如果(obj==此)
返回true;
返回这个.getId()==((学生)obj.getId();
}
}
公共班级学生人数{
公共静态void main(字符串[]args){
学生alex1=新学生(1,“Alex”);
学生alex2=新学生(1,“Alex”);
System.out.println(“alex1 hashcode=“+alex1.hashcode());
System.out.println(“alex2 hashcode=“+alex2.hashcode());
System.out.println(“检查alex1和alex2之间的相等性=“+alex1.equals(alex2));
List studentsLst=newarraylist();
学生名单增补(1);
学生名单增补(2);
System.out.println(“Arraylist size=“+studentsLst.size());
System.out.println(“Arraylist contains Alex=“+studentsLst.contains(新学生(1,“Alex”))));
HashSet students=newhashset();
学生。添加(1);
学生。添加(2);
System.out.println(“HashSet size=“+students.size());
System.out.println(“HashSet contains Alex=“+students.contains(new Student(1,“Alex”))));
}
}
/*
alex1 hashcode=366712642
alex2哈希代码=1829164700
检查alex1和alex2之间的相等性=true
Arraylist大小=2
Arraylist包含Alex=true
哈希集大小=2
HashSet包含Alex=false
*/
当student被插入列表时,我假设它会将它插入两个不同的bucket中,因为hashcode实现不是prsent,默认对象hashcode会被插入,它会将它们存储到两个不同的hash Code中,当搜索另一个对象时,它必须首先通过哈希代码定位bucket,但哈希代码实现不存在,然后列表如何仍然能够识别它,而set却不能


谢谢

列表不使用对象的哈希代码。根据其类型,它们存储对对象的引用数组


如果在列表中调用
contains
,它将使用
.equals()
在列表中迭代以检查对象是否在列表中。

a
list
允许重复。为什么要检查hashcode?
ArrayList
不使用hashcode。通常只有名称中带有“Hash”的集合,例如
HashMap
HashSet
do。