This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes collections- MAP USE Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "collections- MAP USE" Watch "collections- MAP USE" New topic
Author

collections- MAP USE

vijay umar
Ranch Hand

Joined: Mar 24, 2009
Posts: 100
HI EVERYONE

IN THIS HAST MAP PRGRAM I DINT UNDERSTAND SOME COMMANDS: JUS HAVE A LOOK

import java.util.*;
class Dog
{
public Dog(String n) { name = n; }

public String name;

public boolean equals(Object o)
{
if((o instanceof Dog) &&
(((Dog)o).name == name))
{
return true;
}
else
{
return false;
}
}
public int hashCode() {return name.length(); }
}


class Cat { }
enum Pets {DOG, CAT, HORSE }


class MapTest
{
public static void main(String[] args)
{
Map<Object, Object> m = new HashMap<Object, Object>();
m.put("k1", new Dog("aiko")); // add some key/value pairs
m.put("k2", Pets.DOG);
m.put(Pets.CAT, "CAT key");
Dog d1 = new Dog("clover"); // let's keep this reference
m.put(new Cat(), "Cat key");
System.out.println(m.get("k1")); // #1
String k2 = "k2";
System.out.println(m.get(k2)); // #2
Pets p = Pets.CAT;
System.out.println(m.get(p)); // #3
System.out.println(m.get(d1)); // #4
System.out.println(m.get(new Cat())); // #5
System.out.println(m.size()); // #6
}
}


MY DOUBT IS IN THE PROGRAM THE HASHCODE() METHOD WAS USE WHICH IS BEEN OVERRIDEN AND IT RETURNS THE LENGHT OF THE STRING n,
WHERE THIS RETURNED VALUE IS USED ? WHAT IS THE USE OF HAVING THIS RETURNED VALUE IN THE HASHCODE MEHTOD?WHERE DOES IT COMPARE THE OBJECTS?
Ryan Beckett
Ranch Hand

Joined: Feb 22, 2009
Posts: 192
Its called when the hashing structure searches for the object that is a key into the table. Its used to give the structure O(1) search complexity. More specifically, it will tell the structure which hash bucket to look into.

See this hashing tutorial.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

vijay, welcome to JavaRanch.

Please do not write with ALL CAPITALS, IT LOOKS AS IF YOU ARE SHOUTING. Also, please use code tags when you post source code, so that the forum software can show it nicely formatted.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
vijay umar
Ranch Hand

Joined: Mar 24, 2009
Posts: 100
oh i am sorry! i dint know that capital letter should not be used!

doubt:

i just partially understood your statement . can you explain this statement with the help of the same program? will be more clear for me!
Ryan Beckett
Ranch Hand

Joined: Feb 22, 2009
Posts: 192
Well, you should read the tutorial to learn about hashing and the use of hashCode. Run time complexity isn't on the exam, I was just babbling.
armando fonseca
Ranch Hand

Joined: Apr 03, 2009
Posts: 49
Ryan Beckett wrote:Its called when the hashing structure searches for the object that is a key into the table. Its used to give the structure O(1) search complexity. More specifically, it will tell the structure which hash bucket to look into.

See this hashing tutorial.


hi Ryan,

I think rather than give O(1) complexity to the structure, hashes are used to achieve O(1) time complexity, but not all is not guaranteed a O(1) complexity.

-Armando


scjp6-90%
vijay umar
Ranch Hand

Joined: Mar 24, 2009
Posts: 100


in this example the hash code returns a length value!! i am not clear that for which string it returns the length?wether every time a key and value is inserted the length will change or what?
Shasi Mitra
Ranch Hand

Joined: Nov 27, 2008
Posts: 101

Hi Vijay - According to the java API, when you override equals() method you should override hashCode() method too.
Go thru this. You will be able to get..
http://forums.sun.com/thread.jspa?threadID=5299376
 
 
subject: collections- MAP USE
 
Similar Threads
Issue in Working with Maps
working of map?
Doubt in collection
Maps
Bug in a kathy sierra prog...