Two Laptop Bag*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Maps question? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Maps question?" Watch "Maps question?" New topic
Author

Maps question?

sharma ishu
Ranch Hand

Joined: Sep 10, 2012
Posts: 70
class NameCompare implements Comparator<Stuff>{
public int compare(Stuff a, Stuff b){
return b.name.compareTo(a.name);
}
}

This code allows you to use instances of Stuff as keys in Maps or not? And why?
Please help me with this problem.
This problem is from "Practice Exam by Kethy and Bert" 2011. Page 204-Ques.22.
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 739
The NameCompare keys in a TreeMap can be compared based on variable names.
When you put NameCompare keys in TreeMap, the keys are sorted in ascending order.
Ankit Gareta
Ranch Hand

Joined: Mar 28, 2011
Posts: 67

sharma ishu wrote:
This code allows you to use instances of Stuff as keys in Maps or not? And why?


Why it will not allowed to use instance of Stuff as key ?
In any map you can use Stuff as a key, no compilation error.

But in TreeMap , you can't use Stuff object as a key, because in TreeMap the key should be implement comparable interface not comparator, so in TreeMap it gives run-time error...


sharma ishu wrote:
.... problem is from "Practice Exam by Kethy and Bert" 2011. Page 204-Ques.22.


The question you spotted is about sorting, so NameCompare comparator sort the collection in descending order as "return b.name.compareTo(a.name); ".

Hope that will help you.

Thanks,
Ankit


OCPJP 6 (91%)
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Ankit Gareta wrote: because in TreeMap the key should be implement comparable interface not comparator, so in TreeMap it gives run-time error...

You can use Comparator in TreeMap by passing that implementation into TreeMap constructor as in

*remember, TreeMap keep object in some Order(Red-Black tree data structure) so that it can be resulted in sorted manner, so TreeMap uses comparator or comparable.
other map like HashMap iterates unordered fashion and use Array as base data structure, so HashMap dont required this less than and greater than logic .
Ankit Gareta
Ranch Hand

Joined: Mar 28, 2011
Posts: 67

Thanks Seetharaman, I forgot that...
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Ankit Gareta wrote:Thanks Seetharaman

You are welcome
sharma ishu
Ranch Hand

Joined: Sep 10, 2012
Posts: 70
Ankit Gareta wrote:
sharma ishu wrote:
This code allows you to use instances of Stuff as keys in Maps or not? And why?


Why it will not allowed to use instance of Stuff as key ?
In any map you can use Stuff as a key, no compilation error.

But in TreeMap , you can't use Stuff object as a key, because in TreeMap the key should be implement comparable interface not comparator, so in TreeMap it gives run-time error...


sharma ishu wrote:
.... problem is from "Practice Exam by Kethy and Bert" 2011. Page 204-Ques.22.


The question you spotted is about sorting, so NameCompare comparator sort the collection in descending order as "return b.name.compareTo(a.name); ".

Hope that will help you.

Thanks,
Ankit

But the answer in the book is that it can't be used as Key in Map. So, please clarify.
sharma ishu
Ranch Hand

Joined: Sep 10, 2012
Posts: 70
thanks
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18757
    
  40

sharma ishu wrote:Seetharaman Venkatasamy : Could please explain me how Comparable can be used with the TreeMap like you did for the Comparator?


If a TreeMap is configured without a Comparator, the TreeMap will just assume that the elements are Comparable. And as already seen, if the elements are not Comparable, then you will get a cast error.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
 
subject: Maps question?