• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Map Doubt

 
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public static void main( String args[] )
{
Map m = new HashMap();
String str = null;
m.put(new test() , "mill" );
m.put(new test() , "sill" );
System.out.println(m.size());
}
public boolean equals( Object o)
{
return false; // 1
}
public int hashCode()
{
return 0;
}

Choose all that apply.

a.Prints 1
b.Prints 2
c.If Line 1 is replaced with return true; then m.size() returns 1.
d.If hashCode() is not overridden then regardless of whether line 1 returns true or false 2 will be printed on the screen.
e.Compile time error
f.Runtime exception is encountered.
g.If Line 1 is replaced with return true; then size() will not return any number greater than
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Note: Please quote your sources.
 
V Gala
Ranch Hand
Posts: 113
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
cafe4java
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here answers are b and c.

Because when your adding object into the map , map will call equal's method to check whether the given obect is available in the Map or not.
If it is returns true then it will not add otherwise it will add.

In above case it is always returning false menas it will add and prints 2.

In above case it is always returning true menas it will not add and prints 1.
 
What's wrong? Where are you going? Stop! Read this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic