• 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

HashMaps

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Page 626 of SCJP J5 Study Guide (Sierra), I found the following to be irrational associated to the testing of HashMaps...

import java.util.*;

class MapEQ {
public static void main(String [] args) {
Map<ToDos, String> m = new HashMap<ToDos, String>();
String monday=new String("Monday");
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());
}
}

will deliver different results. Knowing how Strings are handled is a requirement for the exam - equals() vs. == .
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

What exactly do you find irrational about this example?

Note that the ToDo class has its implementation for hashCode() commented out, so it is not overriding hashCode(). Instead, it is inheriting Object's implementation of hashCode(). So as explained on page 627, "every entry will go into its own bucket, and the overridden equals() method will have no effect..."
 
Don Solomon
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for your response.

What I find irrational is that one needs to know that the two literals,
"Monday" in t1 == "Monday" in t2; let me stress "==". But a "new String" with the value of "Monday" != t1 | t2. This affects the results of the m.put method. Therefore, when using a "new String" for "Monday" an additional ToDo is added to the HashMap while using a literal, it is not. This is with the hashCode() method un-commented which returns the same value with each invocation.

Thanks

p.s.

The hashCode() for the following are also the same...
String monday=new String("Monday");
System.out.println(monday.hashCode());
System.out.println(("Monday").hashCode());
[ July 20, 2008: Message edited by: Don Solomon ]
 
Ranch Hand
Posts: 210
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

when you are adding entries to HashMap the key of the entries has to implement equals and hashcode, those methods of ToDos class only will have an impact on output,

And please post the complete question ToDos class code is not posted.
[ July 20, 2008: Message edited by: ramesh maredu ]
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how Strings are handled is a requirement for the exam - equals() vs. == is really basic and interesting question. if you omit part of hashmap and if we only talking about String equals vs ==. then

equals for String simply check -- sequence of char used in String if any sequence get mismatch then it`s not equal. like

"kaushik".equlas("kaushik") is true.
"kaushik".equlas("kaushikv") is false,

But in case on == is quite tricky. it checks object reference.

if you take
String s =new String("kaushik");
if("kaushik" == s){
System.out.println(true +"Case 1");
}

if("kaushik" == s.intern()){
System.out.println(true +"Case 2");
}
and will be :- trueCase 2

For this you go thought String pool Concept.

"kaushik" is known as compile time so JVM assign same reference to all "kaushik" string.

while s = new String("kaushik") will create new String object whose reference is different .

when you do s.intern() it will be canonicalized,and you get reference from string pool.

Better read Detail about this concept String canonicalization.
 
Don Solomon
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the responses. I will review their content.

The original question in the book used the double literals for the "Monday". I found that doing a new String("Monday") resulted in
different behavior.

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; }
}

import java.util.*;


class MapEQ {
public static void main(String [] args) {
Map<ToDos, String> m = new HashMap<ToDos, String>();
String monday=new String("Monday");
ToDos t1=new ToDos(monday); // LINE A
ToDos t1=new ToDos("Monday"); // LINE B
ToDos t2=new ToDos("Monday"); // LINE C
ToDos t3=new ToDos("Tuesday");
m.put(t1, "doLaundry");
m.put(t2, "payBills");
m.put(t3, "cleanAttic");
System.out.println(m.size());
}
}

Commenting out LINE A and using LINE B, the Map's size will be 2.
Commenting out LINE B and using LINE A, the Map's size will be 3.

Thank you for your time and consideration.
 
Check your pockets for water buffalo. You might need to use this tiny ad until locate a water buffalo:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic