本文介绍了hascode和equals方法没有被覆盖 - put和get将如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个班学生和商标。
我使用 Student Object 作为 HashMap 并标记为值。
如果我不覆盖 hashMap 和equals,它仍然可以正常工作。

i。如果不覆盖 equals() hashcode() $ b,是否有人可以解释它在内部如何工作? $ b ii。如果我只覆盖 hashcode()



iii.what如果我只覆盖 equals ()

  class Student {

String name;
public String getLastName(){
return lastName;
}
public void setLastName(String lastName){
this.lastName = lastName;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
字符串姓氏;

Student(String name,String lastName){
this.name = name;
this.lastName = lastName;
}
public String toString(){
return(Name:+ this.getName()+Last Name:+ this.getLastName());
}
}
类标记{
学生s;
字符串标记;
public Student getS(){
return s;
}
public void setS(Student s){
this.s = s;
}
public String getMarks(){
return marks;
}
public void setMarks(String marks){
this.marks = marks;
}
标记(学生,字符串标记){
this.marks =标记;
this.s = s;
}
public String toString(){
return(Marks:+ this.getMarks());





public class Main {
public static void main(String [] args){
Student s1 =新学生(Vishnu,Verma);
学生s2 =新生(Amit,Sharma);

标记m1 =新标记(s1,65%);
标记m2 =新标记(s2,67%);

Map< Student,Marks> map = new HashMap< Student,Marks>();
map.put(s1,m1);
map.put(s2,m2);

System.out.println(map);
}
}


解决方案

它如果它们的引用相同,它们会认为这些对象相等即它们指向相同的对象。


I have a class Student and Marks.I am using Student Object as Key for HashMap and Marks as Value.If I don't override hashMap and equals, It still works fine.

i. Can someone please explain how does it internally works on it if not overriding both equals() and hashcode()ii. what If I override only hashcode()

iii.what If I override only equals()

class Student {

    String name;
    public String getLastName() {
        return lastName;
    }
    public void setLastName(String lastName) {
        this.lastName = lastName;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    String lastName;

    Student(String name, String lastName){
        this.name = name;
        this.lastName = lastName;
    }
    public String toString(){
        return(" Name : " + this.getName() + " Last Name : " + this.getLastName());
    }
}
class Marks {
    Student s;
    String marks;
    public Student getS() {
        return s;
    }
    public void setS(Student s) {
        this.s = s;
    }
    public String getMarks() {
        return marks;
    }
    public void setMarks(String marks) {
        this.marks = marks;
    }
    Marks (Student s, String marks){
        this.marks = marks;
        this.s = s;
    }
    public String toString(){
        return(" Marks : " + this.getMarks());
    }
}



public class Main {
    public static void main(String[] args) {
        Student s1 = new Student("Vishnu","Verma");
        Student s2 = new Student("Amit","Sharma");

        Marks m1 = new Marks(s1,"65%");
        Marks m2 = new Marks(s2,"67%");

        Map <Student,Marks>map = new HashMap<Student,Marks>();
        map.put(s1, m1);
        map.put(s2, m2);

        System.out.println(map);
    }
}
解决方案

It will consider the objects equal if their references are equal. i.e. they point to the same object.

这篇关于hascode和equals方法没有被覆盖 - put和get将如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 18:04