• 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
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

collections- MAP USE

 
Ranch Hand
Posts: 100
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 194
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 101
Spring Flex Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
You have to be odd to be #1 - Seuss. An odd little ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic