如果在 Student 类中重写 equals 方法和 hashCode,则 hashmap 中存在多少个对象?

问题描述 投票:0回答:1

如果我有一堂课,假设

Student
课如下:

public class Student {
    private int id;
    private String name;
    private int age;
    public Student(int id, String name, int age) {
    super();
    this.id = id;
    this.name = name;
    this.age = age;
}

那么如果

会有多少个物体

案例1:hashCode和equals方法没有被覆盖

情况2:仅等于方法被覆盖

案例3:仅覆盖hashCode方法

案例4:hashCode和equals方法都被覆盖

情况 5:hashCode 被覆盖,但它始终返回一个常量,例如20,但不等于 覆盖

情况 6:hashCode 被覆盖,但它始终返回一个常量,例如20, 也等于 覆盖

情况 7:等于被覆盖,但它始终返回 true,但 hashCode 未被覆盖

情况 8:等于被覆盖,但它返回始终为 true,hashCode 也被覆盖

情况 9:等于被覆盖,但它始终返回 true,hashCode 也被覆盖,并且始终返回

java object hashmap equals hashcode
1个回答
0
投票
package com.singraul.map;

import java.util.HashMap;
import java.util.Map;

public class CountObject {

public static void main(String[] args) {
    // Case1 : hashCode and equals method not overridden
    empObjectCount();
    // Case2 : only equals method overridden
    stdObjectCount();
    // Case3 : only hashCode method overridden
    teacherObjectCount();
    // Case4 :  hashCode and equals method both overridden
    headObjectCount();
 //Case 5: hashCode overriden but it return always a constant e.g. 
   //20, but equals not overriden
    prinObjectCount();
    //Case 6: hashCode overriden but it return always a constant 
    //e.g. 20, equals also overriden
    sportObjectCount();
    //Case 7: equals overridden but it return always true, but 
    //hashCode not overridden
        mathObjectCount();  
    //Case 8: equals overridden but it return always true, hashCode 
     //also overridden  
        hindiObjectCount();
    //Case 9: equals overridden but it return always true, hashCode 
      //also overridden and it always 
    //  returning constant
        sanskritObjectCount();
}

private static void empObjectCount() {
    System.out.println("empObjectCount ");
    Map<Employee, String> empMap = new HashMap<>();
    empMap.put(new Employee(101, "Rajesh", 33), "employee1");
    empMap.put(new Employee(101, "Rajesh", 33), "employee1");
    empMap.put(new Employee(102, "Amol", 34), "employee2");
    empMap.put(new Employee(103, "Anurag", 30), "employee3");
    // there should be  4 objects, even state of first two objects 
    //are same 
    System.out.println("empMap "+empMap);
    System.out.println("Size of map "+empMap.size());
}

private static void stdObjectCount() {
    System.out.println("stdObjectCount ");
    Map<Student, String> map = new HashMap<>();
    map.put(new Student(101, "Rajesh", 33), "employee1");
    map.put(new Student(101, "Rajesh", 33), "employee1");
    map.put(new Student(102, "Amol", 34), "employee2");
    map.put(new Student(103, "Anurag", 30), "employee3");
    // there should be  4 objects, even state of first two objects 
  //are same 
    System.out.println("map "+map);
    System.out.println("Size of map "+map.size());
}

private static void teacherObjectCount() {
    System.out.println("teacherObjectCount ");
    Map<Teacher, String> map = new HashMap<>();
    map.put(new Teacher(101, "Rajesh", 33), "employee1");
    map.put(new Teacher(101, "Rajesh", 33), "employee1");
    map.put(new Teacher(102, "Amol", 34), "employee2");
    map.put(new Teacher(103, "Anurag", 30), "employee3");
    // there should be  4 objects, even state of first two objects 
    //are same 
    System.out.println("map "+map);
    System.out.println("Size of map "+map.size());
    }

private static void headObjectCount() {
    System.out.println("headObjectCount ");
    Map<HeadMaster, String> map = new HashMap<>();
    map.put(new HeadMaster(101, "Rajesh", 33), "employee1");
    map.put(new HeadMaster(101, "Rajesh", 33), "employee1");
    map.put(new HeadMaster(102, "Amol", 34), "employee2");
    map.put(new HeadMaster(103, "Anurag", 30), "employee3");
    // there should be  3 objects, because state of first two 
 //  objects are same 
    // and hashCode and equals method both overridden 
    System.out.println("map "+map);
    System.out.println("Size of map "+map.size());
    }

   private static void prinObjectCount() {
    System.out.println("prinObjectCount ");
    Map<Principle, String> map = new HashMap<>();
    map.put(new Principle(101, "Rajesh", 33), "employee1");
    map.put(new Principle(101, "Rajesh", 33), "employee1");
    map.put(new Principle(102, "Amol", 34), "employee2");
    map.put(new Principle(103, "Anurag", 30), "employee3");
    // there should be  4 objects, even state of first two objects 
   //are same 
    System.out.println("map "+map);
    System.out.println("Size of map "+map.size());
}

private static void sportObjectCount() {
    System.out.println("sportObjectCount ");
    Map<SportTeacher, String> map = new HashMap<>();
    map.put(new SportTeacher(101, "Rajesh", 33), "employee1");
    map.put(new SportTeacher(101, "Rajesh", 33), "employee1");
    map.put(new SportTeacher(102, "Amol", 34), "employee2");
    map.put(new SportTeacher(103, "Anurag", 30), "employee3");
    // there should be  3 objects, because state of first two 
    //objects are same 
    //  hashCode return only 20 as a constant  and equals method 
    //both overridden 
    System.out.println("map "+map);
    System.out.println("Size of map "+map.size());
}

private static void mathObjectCount() {
    System.out.println("mathObjectCount ");
    Map<MathTeacher, String> map = new HashMap<>();
    map.put(new MathTeacher(101, "Rajesh", 33), "employee1");
    map.put(new MathTeacher(101, "Rajesh", 33), "employee1");
    map.put(new MathTeacher(102, "Amol", 34), "employee2");
    map.put(new MathTeacher(103, "Anurag", 30), "employee3");
    // there should be  4 objects, even state of first two objects 
    //are same 
    //   equals method overridden and returning always true
    System.out.println("map "+map);
    System.out.println("Size of map "+map.size());
}

private static void hindiObjectCount() {
    System.out.println("hindiObjectCount ");
    Map<HindiTeacher, String> map = new HashMap<>();
    map.put(new HindiTeacher(101, "Rajesh", 33), "employee1");
    map.put(new HindiTeacher(101, "Rajesh", 33), "employee1");
    map.put(new HindiTeacher(102, "Amol", 34), "employee2");
    map.put(new HindiTeacher(103, "Anurag", 30), "employee3");
    // there should be  3 objects, because state of first two 
     //objects are same 
    //  equals method  overridden and returning always true and 
     //hashCode also overridden 
    System.out.println("map "+map);
    System.out.println("Size of map "+map.size());
}

private static void sanskritObjectCount() {
    System.out.println("sanskritObjectCount ");
    Map<SanskritTeacher, String> map = new HashMap<>();
    map.put(new SanskritTeacher(101, "Rajesh", 33), "employee1");
    map.put(new SanskritTeacher(101, "Rajesh", 33), "employee1");
    map.put(new SanskritTeacher(102, "Amol", 34), "employee2");
    map.put(new SanskritTeacher(103, "Anurag", 30), "employee3");
    // there should be  1 objects, because state of objects are not 
    //mattering here, As 
    //  equals method overridden and returning always true and 
     //hashCode also overridden 
    // and returning always constant 20
    System.out.println("map "+map);
    System.out.println("Size of map "+map.size());
}
}

class Employee {
private int empId;
private String empName;
private int empAge;

public Employee(int empId, String empName, int empAge) {
    super();
    this.empId = empId;
    this.empName = empName;
    this.empAge = empAge;
}
}

class Student {
private int id;
private String name;
private int age;
public Student(int id, String name, int age) {
    super();
    this.id = id;
    this.name = name;
    this.age = age;
}
@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    Student other = (Student) obj;
    if (age != other.age)
        return false;
    if (id != other.id)
        return false;
    if (name == null) {
        if (other.name != null)
            return false;
    } else if (!name.equals(other.name))
        return false;
    return true;
    }

}
class Teacher {
private int id;
private String name;
private int age;
public Teacher(int id, String name, int age) {
    super();
    this.id = id;
    this.name = name;
    this.age = age;
}
@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + age;
    result = prime * result + id;
    result = prime * result + ((name == null) ? 0 : name.hashCode());
    return result;
}
}

class HeadMaster{
private int id;
private String name;
private int age;
public HeadMaster(int id, String name, int age) {
    super();
    this.id = id;
    this.name = name;
    this.age = age;
}
@Override
public int hashCode() {
    final int prime = 31;
    int result = 1;
    result = prime * result + age;
    result = prime * result + id;
    result = prime * result + ((name == null) ? 0 : name.hashCode());
    return result;
}
@Override
public boolean equals(Object obj) {
    if (this == obj)
        return true;
    if (obj == null)
        return false;
    if (getClass() != obj.getClass())
        return false;
    HeadMaster other = (HeadMaster) obj;
    if (age != other.age)
        return false;
    if (id != other.id)
        return false;
    if (name == null) {
        if (other.name != null)
            return false;
    } else if (!name.equals(other.name))
        return false;
    return true;
    }

  }
 class Principle{
    private int id;
    private String name;
    private int age;
    public Principle(int id, String name, int age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }
    @Override
    public int hashCode() {
        return 20;
    }
}

class SportTeacher{
    private int id;
    private String name;
    private int age;
    public SportTeacher(int id, String name, int age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }
    @Override
    public int hashCode() {
        return 20;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        SportTeacher other = (SportTeacher) obj;
        if (age != other.age)
            return false;
        if (id != other.id)
            return false;
        if (name == null) {
            if (other.name != null)
                return false;
        } else if (!name.equals(other.name))
            return false;
        return true;
    }
}

class MathTeacher{
    private int id;
    private String name;
    private int age;
    public MathTeacher(int id, String name, int age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }
    @Override
    public boolean equals(Object obj) {
        return true;
    }
}
class HindiTeacher{
    private int id;
    private String name;
    private int age;
    public HindiTeacher(int id, String name, int age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + age;
        result = prime * result + id;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        return true;
    }
}
class SanskritTeacher{
    private int id;
    private String name;
    private int age;
    public SanskritTeacher(int id, String name, int age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }
    @Override
    public int hashCode() {
        return 20;
    }
    @Override
    public boolean equals(Object obj) {
        return true;
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.