Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Comparator and Compatable Query

 
Prav sharma
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



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
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Prav sharma
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ankit Garg wrote:The first add operation will generate an exception.


Ankit try this code:


 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prav I think you really need JDK, otherwise how many things you will memorize this way.
 
Sachin Adat
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9528
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Peace please . I will correct that in my post. I said two wrong things in my explanation :XD: :P
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Second question he is asking for this code:


 
Sachin Adat
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Punit & Prav,
You are discussing only this code, or Comparator and Comparable in genral???
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had shown him this code, so he concluded to second option.
 
Prav sharma
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This thread is showing some different icon and the description is HOT. What does it mean?
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It means it has crossed 20 posts.
 
Sachin Adat
Ranch Hand
Posts: 213
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Prav sharma wrote:This thread is showing some different icon and the description is HOT. What does it mean?

It means its hot.............
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic