Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

1. what does the hashcode() method? 2. why we override Equals() method? 3. difference between Iterator & Enumeration?

Hello!! Am pavithra.I have lot of doubts in Spring, Hibernate, Android Am a java developer. I hope it will needfull

user-image
Question added by Pavithra Rani Chamarthi , assistant project , MATRIX TECHNOLOGIES
Date Posted: 2014/06/24
Maria Thomas
by Maria Thomas , Tech Lead , J P Morgan Chase

equals() is a method that forms a part of the java.lang.Object class which is inherited by every object. It basically determines whether two objects are equal. In case of primitive objects like int, char and also special objects like String this equality is quite straightforward and can be used as is. However when we move on to more complex objects like Car, Bus, Animal equals() method does not directly provide a means to determine the equality. In such cases the class needs to override the equals method and provide your own implementation of how it is to be determined.

Eg: class Car{

       private int engineNumber;

       private int wheelSize;

      @Override

       public boolean equals(Object obj){

                 if((this.engineMake == obj.engineMake) && (this.wheelSize == obj.wheelSize))

                     return true;

       }

}

hashcode() method is another such method which comes into significance when dealing with collections like HashTable and HashMap. In such a collection Hash values come into importance. Each object has a hashcode associated with it and two equal objects have the same hashcode. When implementing the equals method, the hashcode should also be overriden to return the same value for similar objects.

Deleted user
by Deleted user

Hashcode() method generates a hashcode for the object which you are creating,now if two  objects are equal their hashcode is always going to be equal.

When we deal with HashMap and Hashtable,Hashcode() is used to locate the object in your collection.

Try and create a hashcode method which could give you a unique value:

public int hashCode() {return42;}

A method like this is always going to return a value42,so this is not the proper implementation of it as every object would fall in the same bucket leading to overhead on your Map impementation(say) when you go on retrieving the object by get()method.

A hashcode() should be like:(food is the instance variable used)

public int hashCode() {

final int prime =31;

int result =1;

result = prime * result + ((food == null) ?0 : food.hashCode());

return result;

}

 

See it as,hashcode() is actually the bucket in which your object falls and equals() method is the one who tries to fetch the value from the bucket.

The more the number of buckets the easy for you to find where the object is kept.Map would already know which bucket to go to rather than just going to one bucket(or rather a large tank) and then looking for the value and then searching through values.

Equals method is overridden when you want to compare the objects.Let's say you create a Car class which stores value of car object as a key and person's name as a value in a Map. Now it's possible for2 guys to have same car(say,Fortuner) but will you fetch the value?answer to this is,just override the equals method and include a VIN(vehicle indentificastion number)which could compare all fortuner objects and give the name of the person corresponding to a particular VIN.

Iterator and Enumerator differences are:

1.the method names have been simplified

Enumeration

hasMoreElement()

nextElement()

N/A 

Iterator

hasNext()

next()

remove() 

The point to consider is addition of remove()method which could help you remove elements from your collection of obejcts.

Though it has some downsides as it could throw ConcurrentModificationException.

2.Iterator are of two types:fail-fast and fail-safe(It's your job to read about them) and it will also let you know what ConcurrentModifcationException is :)))

Also,read about ListIterator.

Wasim Ansari
by Wasim Ansari , Software Engineer (Java) , Interglobe Technologies Pvt Ltd

1) hashCode() :- public int hashCode() it fasilitate searching machanisum in List. When we work with object comparision then ( list.contains(object)) this is not work properly.

Example:-

class Emp{

String name;    int id, salary;   

public Emp(int id, String name, int salary) {      

  this.id = id;        this.salary = salary;        this.name = name;   

}    public void display(){       

System.out.println(id +"  "+name+"  "+salary);   

}}

import java.util.*;

class ListTest {   

public static void main(String arg[]) {       

ArrayList list = new ArrayList();      

  list.add(new Emp(1,"Wasim",55000));     

   list.add(new Emp(2,"Nasim",50000));   

    list.add(new Emp(3,"Nazir",7000));      

  list.add(new Emp(4,"Nasir",70000));               

Emp e1 = new Emp(2,"Nasim",50000);       

if(list.contains(e1)){          

  System.out.println("Found");      

  }        else{          

  System.out.println("Not Found");        }         

   }}

In this program Object e1 having in list until search not successfully.

hashCode() method provide numeric repersentation of object that is stored in index by index in hashTable. We apply serching in hashTable, by equals() method.

 

2) equals() method override to fasilitate content wise comparision of string.

3) Iterator & Enumeration :  Iterator available each Interface & class But Enumeration for only legacy class i.e. Vector.

  Iterator is not thread safe(Concurrent modification are not happened if you try to do concurrentModificationExcption are generated) where as enumeration thread safe(Cuncurrent Modification done.

 

 

 

Mohammad Shoab Ansari Mohammad Shoab Ansari
by Mohammad Shoab Ansari Mohammad Shoab Ansari , Software Developer , Asofttechnotech

In Sort eqals method check the object value containt. So object method also lies in object class. so it is neccessary for override if you check the object value.hash code always given the integer value for hashing. it is best idea for collecting the data. if any case u override the hashCode() method then it is neccessary override the equal method() also.

in last the main difference Enumuration and iteration method. iteration have a remove method while Enumuration does not.

More Questions Like This