hashCode( ) method of List Interface in java

hashCode( ) method of List Interface in java


Posted in : Core Java Posted on : January 5, 2011 at 7:26 PM Comments : [ 0 ]

In the following example we will discuss about hashCode( ) method which are basically overriden to perform certain purpose.

hashCode( ) method of List Interface in java

In the following example we will discuss about hashCode( ) method which are basically overridden to perform certain purpose. Here we will discuss the concept of these methods and why it becomes necessary to override these methods.

Syntax

int hashCode( )

This method returns an integer value.

In this method you will found an integer number. This method provides the hash code of an object. The default implementation of hashCode( ) method uses the 32-bit internal JVM address (an integer value) of the Object as its hashCode . This method is necessary to override because there is a rule of thumb that if you override the method equals( ), you have to override the method hashCode( ). The hashCode method returns the same int Object ID if the two Objects are equal where equality is defined by the method equals( ) and the two different Object ID if they are not equal. i.e. if list1.equals(list2) is true then list1.hashCode( )= = list2.hashCode( ).

Parameter Description

It does not contain any parameter but it returns an integer value as hash code value for the list Object.

Example of hashCode( ) method

In this example we will show you how does hashCode( ) method works in List interface. This example will help you to understand how the same Object Id is  generated for the two different list Objects whose elements are same and the different when these list Objects have different elements. Through this example we will show you why the equals( ) method is required to use for implementing the hashCode( ) method, and how the hashCode( ) method gives an integer value and also find the id of the both lists Object at the 0th position.

Example:-


package Devmanuals.com;
import java.util.List;
import java.util.ArrayList;
public class ListHashCode {
public static void main (String args []){
List l1= new ArrayList();
l1.add("Ram");
l1.add("is");
l1.add("a");
l1.add("boy");
System.out.println("The Element of list1 is : "+l1);
System.out.println("The size of list1 is : "+l1.size());
List l2=new ArrayList();
l2.add("Ram");
l2.add("is");
l2.add("a");
l2.add("boy");
System.out.println("The Element of list2 is : "+l2);
System.out.println("The size of list2 is : "+l2.size());
l1.equals(l2);
System.out.println("The Elements are equal : "+l1.equals(l2));
//When all the elements are equal then hash code is equal
System.out.println("The id of the list1 = "+l1.hashCode());
System.out.println("The id of the list2 = "+l2.hashCode());
System.out.println("The id of Zero position of the list1 = "+l1.get(0).hashCode());
System.out.println("The id of Zero position of the list1 = "+l2.get(0).hashCode());
List l3=new ArrayList();
l3.add("Ram");
l3.add("boy");
System.out.println("The elements of list3 is : "+l3);
System.out.println("The size of list3 is : "+l3.size());
List l4=new ArrayList();
l4.add("Ram");
l4.add("a");
System.out.println("The elements of list4 is : "+l4);
System.out.println("The size of list4 is : "+l4.size());
System.out.println("The Elements are equal : "+l3.equals(l4));
//When all elements are not equal then hash code is different for different list Object
System.out.println("The id of the list3 = "+l3.hashCode());
System.out.println("The id of the list4 = "+l4.hashCode());
}
}

Output :

The Element of list1 is : [Ram, is, a, boy]

The size of list1 is : 4

The Element of list2 is : [Ram, is, a, boy]

The size of list2 is : 4

The Elements are equal : true

The id of the list1 = -1850285320

The id of the list2 = -1850285320

The id of Zero position of the list1 = 81918

The id of Zero position of the list1 = 81918

The elements of list3 is : [Ram, boy]

The size of list3 is : 2

The elements of list4 is : [Ram, a]

The size of list4 is : 2

The Elements are equal : false

The id of the list3 = 2638159

The id of the list4 = 2540516

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics