• 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

Maps

 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello friends,
Can anyone explain me in easy language-step by step how's is this program working !!

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(d1, "Dog key");
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
}
}


which produces something like this:
Dog@1c
DOG
CAT key
Dog key
null
5
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ankit,

Welcome to the Ranch! When you post code, please use the Code button when you are composing your message. That will make your posts much easier to read and it will make it easier for people to help you. Feel free to edit your posts (using the Edit button in your message) and enclose your code within code tags. Also, please make sure to mention in your post the specific source (book, website, or otherwise) where you got the code.

What is it that you don't understand about the workings of the program that you posted?

Thanks!
 
Ankit Dhebar
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK !! I am sorry from next time I will use Code button.
In the Code what i didnt understand is functioning of following block.



I have got this example from BertBates and Kathey Sierra.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are 5 mappings in the map. I think the only confusing thing might be that

returns null.
The reason for that is that the Cat object that you use as a key in the mapping that you create and the Cat object that you use as a key when you attempt to retrieve the mapping are different objects, and since Cat doesn't override hashCode() and equals(), the key won't be found.

When you use an object of a specific class as a key in a map you should ensure that that object's class overrides hashCode() and equals(), or you won't be able to retrieve the mapping. When you don't override hashCode() (used in the first stage in finding the key), the hashCode() defined in the class Object will be used, and that will return different numbers for different objects.

Does that answer your question?
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Dhebar wrote:OK !! I am sorry from next time I will use Code button.



You can use the "edit" button to retroactively code-blockify your original post. I recommend it.

In the Code what i didnt understand is functioning of following block.



The entire block? Surely you understand parts of this, no?

In #1 get() returns the Dog named aiko, which results in something like Dog@xyz because Dog does not override toString().

In #5 get() tries to find a key equal() to new Cat() but doesn't find one. If this is surprising, consider whether new Cat().equals(new Cat()) would return true or false. (And if true, then you would also have to consider hashCode().)

#2, #3, #4, and #6 are pretty straightforward, aren't they?

[edit: I guess I should have tried to compose my post 7 minutes faster. :-)]
 
Brian Cole
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmm, it seems can we no longer delete our own posts.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No problem, Brian. I also agree with you that Ankit could probably be more specific in what he doesn't understand, in order to maximize our chances to solve his doubts.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ruben Soto wrote:I also agree with you that Ankit could probably be more specific in what he doesn't understand



Hey I didn't ask anything
 
Ankit Dhebar
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot to all for helping me !
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Garg wrote:

Ruben Soto wrote:I also agree with you that Ankit could probably be more specific in what he doesn't understand



Hey I didn't ask anything


Ankits everywhere!!! Run, run, run!!!
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ankit Dhebar wrote:Thanks a lot to all for helping me !


Glad that was useful, Ankit!
 
Ankit Dhebar
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ruben Soto wrote:

Ankit Dhebar wrote:Thanks a lot to all for helping me !


Glad that was useful, Ankit!



Yes it was !!!

 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic