aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes HashMaps Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "HashMaps" Watch "HashMaps" New topic
Author

HashMaps

Don Solomon
Ranch Hand

Joined: Jul 20, 2008
Posts: 48
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. == .


Software development is an exercise in thinking not coding.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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..."


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Don Solomon
Ranch Hand

Joined: Jul 20, 2008
Posts: 48
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 ]
ramesh maredu
Ranch Hand

Joined: Mar 15, 2008
Posts: 210

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 ]

SCJP 1.5 94%.
The greatest glory in living lies not in never falling, but in rising every time we fall.
kaushik vira
Ranch Hand

Joined: Feb 01, 2007
Posts: 102
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.


kaushik Vira
-------------------------------------
SCJP, Preparing SCWCD..
Don Solomon
Ranch Hand

Joined: Jul 20, 2008
Posts: 48
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: HashMaps