Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Maps question?

 
sharma ishu
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Seetharaman, I forgot that...
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Gareta wrote:Thanks Seetharaman

You are welcome
 
sharma ishu
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks
 
Henry Wong
author
Marshal
Pie
Posts: 21194
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic