Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * hashCode() is used for bucketing in hash data structures.
- * The value received from hashCode() is used as the bucket number for storing elements of the set/map.
- * This bucket number is the address of the element inside the set/map.
- *
- * When you do contains() it will take the hash code of the element, then look for the bucket where hash code points to.
- * 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,
- * and then decide if contains() is true or false, or decide if element could be added in the set or not.
- */
- // Even when emp1 and emp2 objects are the same, the JVM doesn't recognize
- // 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
- EmployeeEqualsAndHashcode emp1 = new EmployeeEqualsAndHashcode(101, "Jim", "Carrey");
- EmployeeEqualsAndHashcode emp2 = new EmployeeEqualsAndHashcode(101, "Jim", "Carrey");
- EmployeeEqualsAndHashcode emp3 = new EmployeeEqualsAndHashcode(102, "Buba", "Mara");
- EmployeeEqualsAndHashcode emp4 = new EmployeeEqualsAndHashcode(103, "Martin", "Scorzese");
- System.out.println(emp2.equals( emp1 ));
- /**
- * Hashcode is all different for all the objects ( no matter if there information is the same )
- */
- System.out.println("hashCode for emp1 ---> " + emp1.hashCode());
- System.out.println("hashCode for emp2 ---> " + emp2.hashCode());
- System.out.println("hashCode for emp3 ---> " + emp3.hashCode());
- System.out.println("hashCode for emp4 ---> " + emp4.hashCode());
- /**
- * In case of using collections like HashSet and HashMap
- * the hashCode is going to be used to put them in a particular bucket
- */
- Set<EmployeeEqualsAndHashcode> employees = new HashSet<>();
- employees.add( emp1 );
- employees.add( emp2 );
- employees.add( emp3 );
- employees.add( emp4 );
- System.out.println("\nEmployee set size: " + employees.size());
- Map<EmployeeEqualsAndHashcode, EmployeeEqualsAndHashcode> employeeMap = new HashMap<>();
- employeeMap.put( emp1, emp1 );
- employeeMap.put( emp2, emp2 );
- employeeMap.put( emp3, emp3 );
- employeeMap.put( emp4, emp4 );
- System.out.println("\nEmployee map size: " + employeeMap.size());
- }
- private int id;
- private String name;
- private String lastName;
- public EmployeeEqualsAndHashcode() {}
- public EmployeeEqualsAndHashcode(int id, String name, String lastName) {
- this.id = id;
- this.name = name;
- this.lastName = lastName;
- }
- 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;
- }
- public String getLastName() {
- return lastName;
- }
- public void setLastName( String lastName ) {
- this.lastName = lastName;
- }
- @Override public String toString() {
- final StringBuilder sb = new StringBuilder( "Employee{" );
- sb.append( "id=" ).append( id );
- sb.append( ", name='" ).append( name ).append( '\'' );
- sb.append( ", lastName='" ).append( lastName ).append( '\'' );
- sb.append( '}' );
- return sb.toString();
- }
- @Override public boolean equals( Object o ) {
- if( this == o ) {
- return true;
- }
- if( o == null || getClass() != o.getClass() ) {
- return false;
- }
- EmployeeEqualsAndHashcode employee = (EmployeeEqualsAndHashcode) o;
- return id == employee.id &&
- Objects.equals( name, employee.name ) &&
- Objects.equals( lastName, employee.lastName );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement