Sunday, August 7, 2016

Object Equality

Every Java object inherits a set of base methods from java.lang.Object

Creational methods
Object()   - Default no-argument constructor
clone()     - Returns a new instance of the class

Synchronizing methods
notify() - Sends a signal to a waiting thread (on the current instance)
notifyAll() - Sends a signal to all waiting threads (on the current instance)
wait() - Forces the current thread to wait for a signal (on the current instance)

Equality methods
equals(Object) - Returns true if this instance is equal to the argument
hashCode() - Returns a hash code based on the instance data

Other methods
toString() -Returns a string representation of the object
finalize() - Performs garbage-collection duties
getClass() - Returns the Class object associated with the instance


public class Test{
public static void main(String args[]){
System.out.println(new N()==(new N())); //false
System.out.println(new N().equals(new N())); //false
}
}
class N{}

public class Point {
  private static double version = 1.0;
  private transient double distance;
  private int x, y;
  public boolean equals(Object other) {
    if (other == this) return true;
    if (other == null) return false;
    if (getClass() != other.getClass()) return false;
    Point point = (Point)other;
    return (x == point.x && y == point.y);
  }
}
 Importantly, if two instances are considered equal by equals(), then they must have the same hash code.

The general contract of hashCode is: Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer,

 hashcode does nothing with your business logic, we have to take care it in most cases. Because when your object is put into a hash based container(HashSet, HashMap...), the container puts/gets the element's hashcode.

equals()
equals() is used in most collections to determine if a collection contains a given element. For instance:

List list = new ArrayList();
list.add("123");
boolean contains123 = list.contains("123");

public class Test{
public static void main(String args[]){
List p = new ArrayList();
p.add(new Point());
if(!p.contains(new Point())){
p.add(new Point());
}
System.out.println(p.size());//1
}
}
class Point {
  private static double version = 1.0;
  private transient double distance;
  private int x, y;
  public boolean equals(Object other) {
    if (other == this) return true;
    if (other == null) return false;
    if (getClass() != other.getClass()) return false;
    Point point = (Point)other;
    return (x == point.x && y == point.y);
  }
}

public class Employee {
  ...
  public boolean equals(Object o){
    if(o == null)                return false;
    if(!(o instanceof) Employee) return false;

    Employee other = (Employee) o;
    if(this.employeeId != other.employeeId)      return false;
    if(! this.firstName.equals(other.firstName)) return false;
    if(! this.lastName.equals(other.lastName))   return false;

    return true;
  }
}


No comments:

Post a Comment

உப்பு மாங்காய்

சுருக்குப்பை கிழவி. சுருக்கங்கள் சூழ் கிழவி. பார்க்கும் போதெல்லாம் கூடையுடனே குடியிருப்பாள். கூடை நிறைய குட்டி குட்டி மாங்காய்கள். வெட்டிக்க...