当使用Map时,containsKey在Java中是如何工作的?

当使用Map时,containsKey在Java中是如何工作的?,java,Java,containsKey是如何工作的?我知道如果我这样做: Map<String, Integer> map = new HashMap<>(); map.put("user1", 1); map.put("user2", 2); map.put("user3", 3); System.out.println(map.containsKey("user1")); // true Map<Person, Integer> table = new Ha

containsKey是如何工作的?我知道如果我这样做:

 Map<String, Integer> map = new HashMap<>();
 map.put("user1", 1);
 map.put("user2", 2);
 map.put("user3", 3);

 System.out.println(map.containsKey("user1")); // true
Map<Person, Integer> table = new HashMap<>();
table.put(new Person("Steve"), 33);
table.put(new Person("Mark"), 29);

System.out.println(table.containsKey(new Person("Steve"))); // false
Map Map=newhashmap();
map.put(“user1”,1);
地图放置(“用户2”,2);
地图放置(“用户3”,3);
System.out.println(map.containsKey(“user1”);//真的
containsKey返回true

但如果我这样做:

 Map<String, Integer> map = new HashMap<>();
 map.put("user1", 1);
 map.put("user2", 2);
 map.put("user3", 3);

 System.out.println(map.containsKey("user1")); // true
Map<Person, Integer> table = new HashMap<>();
table.put(new Person("Steve"), 33);
table.put(new Person("Mark"), 29);

System.out.println(table.containsKey(new Person("Steve"))); // false
Map table=newhashmap();
表1.put(新人(“史蒂夫”),33岁;
表1.put(新人(“马克”),29;
System.out.println(table.containsKey(新人(“史蒂夫”));//假的

那么,为什么即使我有正确的密钥,我也会得到false?如何使用33的键检查其值?

这里使用字符串作为键

Map Map=newhashmap()

map.put(“user1”,1)

String
ia类实现了
HashCode
Equals
方法,因此它可以按预期工作

当您使用
Person
类对象作为键或任何自定义类时,应确保重写
Hashcode
equals
方法


HashMap
实现使用
hashCode
查找bucket,如果bucket中存在多个条目,则使用
等于

这里使用字符串作为键

Map Map=newhashmap()

map.put(“user1”,1)

String
ia类实现了
HashCode
Equals
方法,因此它可以按预期工作

当您使用
Person
类对象作为键或任何自定义类时,应确保重写
Hashcode
equals
方法


HashMap
实现使用
hashCode
查找bucket,如果bucket中存在多个条目,则使用
等于

使用map时,需要存储在map中的所有对象都应实现等于和hashCode

下面是一个示例Person类,其行为与您预期的一样:

class Person {
        private String name;

        public Person(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null || !(obj instanceof Person)) {
                return false;
            }

            Person other = (Person) obj;

            return other.getName().equals(this.name);
        }

        @Override
        public int hashCode() {
            return this.name.hashCode();
        }

    }
HashMap使用equals()比较键是否相等
hashCode()用于计算项目应插入的索引

使用地图时,需要存储在地图中的所有对象应实现等于和hashCode

下面是一个示例Person类,其行为与您预期的一样:

class Person {
        private String name;

        public Person(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == null || !(obj instanceof Person)) {
                return false;
            }

            Person other = (Person) obj;

            return other.getName().equals(this.name);
        }

        @Override
        public int hashCode() {
            return this.name.hashCode();
        }

    }
HashMap使用equals()比较键是否相等
hashCode()用于计算项目应插入的索引

,因为您的
Person
类没有(正确地)实现
hashCode()
equals()
。因为您没有在类上实现
equals()
,Java只能检查这两个对象是否引用相同的内存。这解释了等式在Java中是如何工作的,因为您的
Person
类没有(正确地)实现
hashCode()
equals()
。因为您没有在类上实现
equals()
,Java只能检查这两个对象是否引用了相同的内存。这解释了平等在Java中的工作原理