*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Comparator and Compatable Query Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Comparator and Compatable Query" Watch "Comparator and Compatable Query" New topic
Author

Comparator and Compatable Query

Prav sharma
Ranch Hand

Joined: Feb 07, 2005
Posts: 102



Choose 3 that apply.

a. Compiles without warnings
b. Compiles with warnings
c. Only one test Object is added, the other one is considered a duplicate.
d. A Runtime Exception occurs.
e. TreeSet can only accept objects of type test. ( Given that test has no sub classes )

The answers are a,d and e.

1) Why not C is considered as Correct???
2) Why comparable is needed here. Is using comparator wrong?



Source : WHizlabs

Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Well c is wrong as no object will be added to the Set. The second add operation will generate an exception. For elements of a class to be added to a sorted collection like TreeSet, the class must implement Comparable not Comparator. If the class doesn't implement Comparable, then you will have to use a comparator to add elements of that class into the collection but then the code will loke like this

Set < test > s1 = new TreeSet < test > (new test()); //passing the comparator to the constructor


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
1) Why not C is considered as Correct???


As it will throw ClassCastException at runtime, so option c is wrong.

2) Why comparable is needed here. Is using comparator wrong?

If using comparator than say TreeSet that you want to use Comparator by passing comparator object to TreeSet(Comparator) constructor, otherwise TreeSet try to convert your object to Comparable, that will throw ClassCastException.


SCJP 6
Prav sharma
Ranch Hand

Joined: Feb 07, 2005
Posts: 102
Ankit Garg wrote:Well c is wrong as no object will be added to the Set. The first add operation will generate an exception. For elements of a class to be added to a sorted collection like TreeSet, the class must implement Comparable not Comparator. If the class doesn't implement Comparator, then you will have to use a comparator to add elements of that class into the collection but then the code will loke like this

Set < test > s1 = new TreeSet < test > (new test()); //passing the comparator to the constructor


Is the text in Green color above right? I didn't understand it properly.
Could you please clarify?

For Sorted Set also comparable rather than Comparator is needed to be able to add? Then when is comparaor needed?
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Ankit Garg wrote:The first add operation will generate an exception.


Ankit try this code:


Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Prav I think you really need JDK, otherwise how many things you will memorize this way.
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
Ankit Garg wrote:If the class doesn't implement Comparator, then you will have to use a comparator to add elements of that class into the collection

I think you mean Comparable here.(typo)


SCJP 6
How To Ask Questions On Java Ranch - How To Answer Questions On Java Ranch
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
It's a typo Prav:
Actually Ankit wanted to write comparable but wrote comparator:

If the class doesn't implement Comparable, then you will have to use a comparator to add elements of that class into the collection but then the code will loke like this
Prav sharma
Ranch Hand

Joined: Feb 07, 2005
Posts: 102
Punit Singh wrote:Prav I think you really need JDK, otherwise how many things you will memorize this way.


I understand its tough. But i need to manage untill i really get that.
Prav sharma
Ranch Hand

Joined: Feb 07, 2005
Posts: 102
1. import java.util.*;
2. class test implements Comparator < test >
3. {
4. int testNumber;
5. public static void main( String args[] )
6. {
7. Set < test > s1 = new TreeSet < test > (new Test());
8.
9. s1.add(new test());
10. s1.add(new test());
11.
12. }
13. public int compare( test t1 , test t2 )
14. {
15. return t1.testNumber - t2.testNumber;
16. }
17.
18. }

Ok , if the code is modified as this, then it will work???

RED Lines indicate update.
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
Prav sharma wrote:RED Lines indicate update.

I cant see anything in red......... , but I got the change you are mentioning, and that's what Ankit said........

Ohhh......... ops:
please use code buttons, or my message is meaning less........
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952


Prav if you comment second line, then also it will run fine here, but it is hidden concept, and not very important.
Prav sharma
Ranch Hand

Joined: Feb 07, 2005
Posts: 102
1) If a class is implementing comparator and not implementing comparable then can it be added to TreeSet and TreeMap? Above example confuses me. I thought the answer is no. But you told that one add statment works?

Could you please clarify?
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Peace please . I will correct that in my post. I said two wrong things in my explanation :XD: :P
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Leave it, it is just a hidden thing I found when I was moving through source code of TreeSet. Do not worry, when you will have your JDK, you can see this also.

When you add first object TreeSet add() simply adds that object in the TreeSet.
Actually when you add second object in TreeSet, then only TreeSet add() does something like this:



Comparable c=(Comparable)firstObject; this is done only when you add second Object, so it will not throw any ClassCastException if you add only one object.
Prav sharma
Ranch Hand

Joined: Feb 07, 2005
Posts: 102
Ok.

I'll Summarise

a) Comparable is needed for classes to implement, who are getting added to TreeSet or TreeMap.
b) Need for comparable arises when more than one object is added.

Please correct if i am wrong.

What are other places where classes need to implement Comparable before addition ?
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Prav wrote:
What are other places where classes need to implement Comparable before addition ?


No, no more places where you need. It is only for Treexxx.
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
Prav sharma wrote:
a) Comparable is needed for classes to implement, who are getting added to TreeSet or TreeMap.

Wrong(Not just TreeSet or TreeMap)
The Comparable interface is used by the Collections.sort() method and the java.util.Arrays.sort() method to sort Lists and Arrays of Objects, respectively.
Even if you want to use the sort method you have to implement Comparable.
Prav sharma wrote:
b) Need for comparable arises when more than one object is added.

Wrong
The sort() method has an overloaded version of sort()that takes List and Comparator.
Now a TreeSet keeps your collection sorted. So I guess it implicitly calls the sort method.
So if you are adding objects to a TreeSet, which do not implement Comparable, you can pass the Comparator object to tell the TreeSet object how to sort your objects.
So for sorting any collection, the objects in the collections should be comparable(either implement Comparable or create a class Comparator which compares)
TreeSet keeps objects in a sorted manner, so you need to do the above.

In your case, you have the same class that you are adding to the Collection, implement the Comparator. So you need to specify while defining the collection that your object and the comparator are in the same class.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Second question he is asking for this code:


Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
Punit & Prav,
You are discussing only this code, or Comparator and Comparable in genral???
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
I had shown him this code, so he concluded to second option.
Prav sharma
Ranch Hand

Joined: Feb 07, 2005
Posts: 102
This thread is showing some different icon and the description is HOT. What does it mean?
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
It means it has crossed 20 posts.
Sachin Adat
Ranch Hand

Joined: Sep 03, 2007
Posts: 213
Prav sharma wrote:This thread is showing some different icon and the description is HOT. What does it mean?

It means its hot.............
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Comparator and Compatable Query