• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

doubt in hashCode and Equals method

 
Ranch Hand
Posts: 122
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have written a small programe to test equals and HashCode method in arrayList

public static void main(String[] args)
{

TestVO t1 = new TestVO() ;
TestVO t2 = new TestVO() ;

ArrayList a = new ArrayList() ;
a.add(t1);
a.add(t2) ;

System.out.println("=== " + a);
a.remove(t2) ;
System.out.println("=== " + a);
}


my TestVO class is

public class TestVO {

public int hashCode()
{
return (int) (Math.random() * 100000 );
}

public boolean equals(Object o)
{
return true ;
}

}

when i run the programme i get the output

=== [com.tagLibrary.TestVO@e3f3, com.tagLibrary.TestVO@39d5]
=== [com.tagLibrary.TestVO@10b62]


it seems that when i remove an object from the list, all objects from the list is removed and again created(different hashCode).

can any one what has happened and why i am getting different hashCode .. ?


 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to javaranch Hemant. Can you please post your code inside the CODE tags ? It will make it easier to read the code
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

why i am getting different hashCode .. ?


Because your hashCode method returns a random hash code. Try this code

I'm sure you'll get a different output for line 1 and 2 i.e. even if you don't remove anything from the ArrayList, you'll get a different output...
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Deepak Bala wrote:Welcome to javaranch Hemant.


If you look, Hemant has been here for quite a while , its his first post after registering 11 months ago. So congrats Hemant on your first post
 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

If you look, Hemant has been here for quite a while , its his first post after registering 11 months ago.



Yeah I realized that when I saw his registration date. The forum has a lot of users that watch silently and try to learn from someone else's questions. Congrats on your first post Hemant
 
We begin by testing your absorbancy by exposing you to this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic