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

collection and generics  RSS feed

 
Ranch Hand
Posts: 69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello ,
this question is from k& b book form topic 7 Generics and collection qs no 7 , can some one explain the flow of this i am not getting


import java.util.*;
class MapEQ
{
public static void main(String[] args)
{
Map<ToDos , String > m = new HashMap<ToDos , String>();
ToDos t1 = new ToDos("Monday");
ToDos t2 = new ToDos("Monday");
ToDos t3 = new ToDos("Tuesday");

m.put(t1 , "doLaundry");
m.put(t2 , "payBills");
m.put(t3 , "cleanAttic");

}
}

class ToDos {
String day;
ToDos(String d )
{
day = d;
}

public boolean equals(Object o) {
return((ToDos)o).day==this.day'
}

// public int hashCode()
{
return 9;
}
}

o/p

a. As the code stands it will not compile

b. As the code stands the output will be 2
c. As the code stands the output will be 3
d. if the hshCode() method is uncommented the output will be 2
e. if the hshCode() method is uncommented the output will be 3
f. if the hshCode() method is uncommented code s will not compile

c and d are the right options of this question

thanks in advance
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can the search keyword "MapEQ" using the search function on the top right of pages and find a couple of topics about this question.
Here is the Link
 
Ranch Hand
Posts: 510
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens when you test this code yourself, What happens when you run it in debug and make the changes that the question asks?
 
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


in this code you are using ToDos class objects as your key. In HashMap key and value must be unique.
You have defined your own uniquness about ToDos object by overriding the equals and hashCode method.
Your equals() in ToDos class says that two objects equal if they have the same value for day...

Since it is a HashMap so to test for equality HshMap will be needing both hashCode as well as equals method...

In the first case you have commented the hashCode()..so HashMap calls the Object class's hashCode()..which always gives a unique value..
Since you get a unique value from hashCode()..immediately your objects become unique irrespective of what you have written in equals()..because remember
first the hashCode() is called and then the equals()..
if hashCode() returns unique value for to different objects they will never be equal...
hence the o/p is 3..

in next case when you uncomment the hashCode()..it returns a same number for all three ToDos object..
so now your equals() will be called..

but now according to equals() method t1 & t2 become equal because they have the same value for "day"...
only one of them is added..

hence 'd' is also correct...
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
could anyone tell me from where does the outout come.
There is no system.out.print statement in the code.What is actually printed


Sumi
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi sumi,

yes, the code is incomplete. At the end of the main method the size of the map is printed.
 
I'm not sure if I approve of this interruption. But this tiny ad checks out:
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!