This week's book giveaway is in the Spring forum.
We're giving away four copies of Modern frontends with htmx and have Wim Deblauwe on-line!
See this thread for details.
Win a copy of Modern frontends with htmx this week in the Spring forum!
  • 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

A Question about Java Maps

 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The following Java code is based upon a question from the testing softare that came with the book "Sun Certified Programmer for Java 5" by K Sierra and B Bates. However, I have modified the code. Here is my version of
the code:

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" );
System.out.println( m.size() );
}
}

class ToDos {
public boolean equals( Object o1 )
{
System.out.println( "equals called: ");
if ( !(o1 instanceof ToDos) )
return false;
ToDos dos1 = (ToDos)o1;
if ( task.equals( dos1.task ) )
return true;
return false;
}
public int compareTo( ToDos dos1 )
{
System.out.println( "compareTo called: ");
System.out.println( task );
System.out.println( dos1.task );
System.out.println( task.compareTo( dos1.task ) );
return task.compareTo( dos1.task );
}
String task;
ToDos(String t)
{ task = t; };
}

The first change was to fix the syntax error. This code now compiles. However, it prints out 3 for the size of the map. I would like it to print out 2. That is, I want the code to consider t1 and t2 to be the same object since they have the same value for the key. How do I do this?

Thanks
Bob
 
author
Posts: 23950
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You forgot to override the hashCode() method. In order to use an object as a key to a hashmap, you need to comform to the equals() - hashCode() contract.

Henry
 
Bob Sherry
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the response. I added the following code to the class ToDos:

public int hash( ToDos o1 )
{
System.out.println( "hash called" );
return 0;
}

However, the string hash called did not appear in the output. I also tried
making the argument of type Object. That did not work either. The output is still 3. Any other ideas?

Thanks
Bob
 
Henry Wong
author
Posts: 23950
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would help if you named the method correctly. The method is named hashCode().

Henry
 
Bob Sherry
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Henry,

Thanks for the response. I changed the name to hashCode and that still did not work. Any other ideas?

Bob
 
Henry Wong
author
Posts: 23950
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No offense, but you are not trying very hard. A quick google would have told you that the signature is also incorrect.

Henry
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic