Win a copy of Machine Learning Systems: Designs that scale this week in the Scala forum
or Xamarin in Action: Creating native cross-platform mobile apps in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Clone method showing different values for different programs.  RSS feed

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My task is to
1. Instantiate an object(a)
2. declare an other of the same class(b)
3. clone the object in step 1 to step 2(b=a.clone())
4. check if a and b are equal(a== b and a.equals(b))
5 Declare a hashset and add both a and b to the hashset.

Now my issues are as below
1. For Code 1 below HashSet is showing 1 as the count
2. For Code 2 below HashSet is showing 2 as the count.
Code1


Code 2
 
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

VenkataPrasad Regula wrote:



You probably want to make the second assignment like y = b.hashcode(); Anyways, if you want your objects to behave properly when put into HashSets then you need to properly override the equals() and hashCode() methods. See this journal article for details: http://www.javaranch.com/journal/2002/10/equalhash.html
 
The world's cheapest jedi mind trick: "Aw c'mon, why not read this tiny ad?"
Rocket Oven Kickstarter - from the trailboss
https://coderanch.com/t/695773/Rocket-Oven-Kickstarter-trailboss
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!