Advertisement
PetkoTrenev

Equals and Hashcode

Jan 29th, 2020
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.74 KB | None | 0 0
  1. /**
  2.      * hashCode() is used for bucketing in hash data structures.
  3.      * The value received from hashCode() is used as the bucket number for storing elements of the set/map.
  4.      * This bucket number is the address of the element inside the set/map.
  5.      *
  6.      * When you do contains() it will take the hash code of the element, then look for the bucket where hash code points to.
  7.      * If more than 1 element is found in the same bucket (multiple objects can have the same hash code), then it uses the equals() method to evaluate if the objects are equal,
  8.      * and then decide if contains() is true or false, or decide if element could be added in the set or not.
  9.      */
  10.  
  11.     // Even when emp1 and emp2 objects are the same, the JVM doesn't recognize
  12.     // that is why we have to override equals/hashCode to be sure that the objects we want to compare are equal according to my preferences
  13.     EmployeeEqualsAndHashcode emp1 = new EmployeeEqualsAndHashcode(101, "Jim", "Carrey");
  14.     EmployeeEqualsAndHashcode emp2 = new EmployeeEqualsAndHashcode(101, "Jim", "Carrey");
  15.     EmployeeEqualsAndHashcode emp3 = new EmployeeEqualsAndHashcode(102, "Buba", "Mara");
  16.     EmployeeEqualsAndHashcode emp4 = new EmployeeEqualsAndHashcode(103, "Martin", "Scorzese");
  17.  
  18.     System.out.println(emp2.equals( emp1 ));
  19.  
  20.     /**
  21.      * Hashcode is all different for all the objects ( no matter if there information is the same )
  22.      */
  23.     System.out.println("hashCode for emp1 ---> " + emp1.hashCode());
  24.     System.out.println("hashCode for emp2 ---> " + emp2.hashCode());
  25.     System.out.println("hashCode for emp3 ---> " + emp3.hashCode());
  26.     System.out.println("hashCode for emp4 ---> " + emp4.hashCode());
  27.  
  28.  
  29.     /**
  30.      * In case of using collections like HashSet and HashMap
  31.      * the hashCode is going to be used to put them in a particular bucket
  32.      */
  33.     Set<EmployeeEqualsAndHashcode> employees = new HashSet<>();
  34.     employees.add( emp1 );
  35.     employees.add( emp2 );
  36.     employees.add( emp3 );
  37.     employees.add( emp4 );
  38.  
  39.     System.out.println("\nEmployee set size: " + employees.size());
  40.  
  41.  
  42.     Map<EmployeeEqualsAndHashcode, EmployeeEqualsAndHashcode> employeeMap = new HashMap<>();
  43.     employeeMap.put( emp1, emp1 );
  44.     employeeMap.put( emp2, emp2 );
  45.     employeeMap.put( emp3, emp3 );
  46.     employeeMap.put( emp4, emp4 );
  47.  
  48.     System.out.println("\nEmployee map size: " + employeeMap.size());
  49.   }
  50.  
  51.   private int id;
  52.   private String name;
  53.   private String lastName;
  54.  
  55.   public EmployeeEqualsAndHashcode() {}
  56.  
  57.   public EmployeeEqualsAndHashcode(int id, String name, String lastName) {
  58.     this.id = id;
  59.     this.name = name;
  60.     this.lastName = lastName;
  61.   }
  62.  
  63.   public int getId() {
  64.     return id;
  65.   }
  66.  
  67.   public void setId( int id ) {
  68.     this.id = id;
  69.   }
  70.  
  71.   public String getName() {
  72.     return name;
  73.   }
  74.  
  75.   public void setName( String name ) {
  76.     this.name = name;
  77.   }
  78.  
  79.   public String getLastName() {
  80.     return lastName;
  81.   }
  82.  
  83.   public void setLastName( String lastName ) {
  84.     this.lastName = lastName;
  85.   }
  86.  
  87.   @Override public String toString() {
  88.     final StringBuilder sb = new StringBuilder( "Employee{" );
  89.     sb.append( "id=" ).append( id );
  90.     sb.append( ", name='" ).append( name ).append( '\'' );
  91.     sb.append( ", lastName='" ).append( lastName ).append( '\'' );
  92.     sb.append( '}' );
  93.     return sb.toString();
  94.   }
  95.  
  96.   @Override public boolean equals( Object o ) {
  97.     if( this == o ) {
  98.       return true;
  99.     }
  100.     if( o == null || getClass() != o.getClass() ) {
  101.       return false;
  102.     }
  103.     EmployeeEqualsAndHashcode employee = (EmployeeEqualsAndHashcode) o;
  104.     return id == employee.id &&
  105.         Objects.equals( name, employee.name ) &&
  106.         Objects.equals( lastName, employee.lastName );
  107.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement