• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Maps question?

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 2447
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 67
Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Seetharaman, I forgot that...
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
reply
    Bookmark Topic Watch Topic
  • New Topic