• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

collections- MAP USE

 
vijay umar
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15448
42
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
vijay umar
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 192
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
vijay umar
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 101
Flex Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic