aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Maps Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Maps" Watch "Maps" New topic
Author

Maps

Ankit Dhebar
Greenhorn

Joined: Feb 09, 2009
Posts: 16
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
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
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!


All code in my posts, unless a source is explicitly mentioned, is my own.
Ankit Dhebar
Greenhorn

Joined: Feb 09, 2009
Posts: 16
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

Joined: Dec 16, 2008
Posts: 1032
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?
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
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. :-)]

bitguru blog
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
hmm, it seems can we no longer delete our own posts.
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
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.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

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


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ankit Dhebar
Greenhorn

Joined: Feb 09, 2009
Posts: 16
Thanks a lot to all for helping me !
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
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

Joined: Dec 16, 2008
Posts: 1032
Ankit Dhebar wrote:Thanks a lot to all for helping me !

Glad that was useful, Ankit!
Ankit Dhebar
Greenhorn

Joined: Feb 09, 2009
Posts: 16
Ruben Soto wrote:
Ankit Dhebar wrote:Thanks a lot to all for helping me !

Glad that was useful, Ankit!


Yes it was !!!
 
 
subject: Maps