File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes generic comparator problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "generic comparator problem" Watch "generic comparator problem" New topic
Author

generic comparator problem

Sigrid Kajdan
Ranch Hand

Joined: Jan 14, 2007
Posts: 72
Hello,
trying to compile the following code, I get the following error:

The method valueOf(String) in the type Integer is not applicable for the arguments (String)


I have no idea what the problem is here - can anyone give me hint?

Also, the compiler warns me that

The type parameter String is hiding the type String


Here's the code:

public class KondiComparator<String> implements Comparator<String> {

public int compare(String o1, String o2) {
return compare(Integer.valueOf(o1), Integer.valueOf(o2));
}

}

Excuse me if these are stupid questions - I still have problems getting generics...

Thanks in advance,
Sigrid
Garrett Rowe
Ranch Hand

Joined: Jan 17, 2006
Posts: 1296

Your Comaparater class doesn't need/ use its parameterized type. It only has to declare that it implements Comparator<String>. Also you haven't defined a method compare which take two Integer arguments so the compiler chokes on compare(Integer, Integer).


Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peter
Sigrid Kajdan
Ranch Hand

Joined: Jan 14, 2007
Posts: 72
Also you haven't defined a method compare which take two Integer arguments so the compiler chokes on compare(Integer, Integer).


Oh, that must have been a lapse after a hard working day... But I still need help on the second point:

Your Comaparater class doesn't need/ use its parameterized type. It only has to declare that it implements Comparator<String>.


What exactly is the difference between
public class KondiComparator<String> implements Comparator<String> and
public class KondiComparator implements Comparator<String> ?

(That is, what would I write the first one for?)

Also, I noticed that if I reinsert the <String> into your correct code, I get the problem with the compareTo-method again, even though the method itself is correct now:

public class KondiComparator<String> implements Comparator<String> {

public int compare(String o1, String o2) {
return Integer.valueOf(o1).compareTo(Integer.valueOf(o2));
}

}
Could you please explain what's happening here?
Manju Devarla
Ranch Hand

Joined: Dec 14, 2006
Posts: 85
When we are implementing Comparator,class must implement a single method called comapre()

same way When we are implementing Comparable,class must implement a single method called comapreTo() not viceversa
Sigrid Kajdan
Ranch Hand

Joined: Jan 14, 2007
Posts: 72
Excuse me, but still I don't understand.

So, the following code compiles:

public class KondiComparator implements Comparator<String> {

int x = Integer.valueOf("String");

public int compare(String o1, String o2) {
return Integer.valueOf(o1).compareTo(Integer.valueOf(o2));
}

}

But this doesn't: (using KondiComparator<String> instead of KondiComparator)

public class KondiComparator<String> implements Comparator<String> {

int x = Integer.valueOf("String");

public int compare(String o1, String o2) {
return Integer.valueOf(o1).compareTo(Integer.valueOf(o2));
}

}

But, changing the method to simply return 1, the KondiComparator<String> compiles:

public class KondiComparator<String> implements Comparator<String> {

int x = Integer.valueOf("String");

public int compare(String o1, String o2) {
return 1;
}

}

So, I wonder what difference there is between

KondiComparator<String> implements Comparator<String> and KondiComparator implements Comparator<String> ...
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
I'll stick my neck out on this one.

In this:


you are defining a generic type with one type parameter String. That is not class String but just an identifier. You could replace it throughout by another symbol, T say. So the code becomes:


That's why you are getting (in your original code) the compiler message "The type parameter String is hiding the type String"

The above is very different to:


which is defining a non-generic type implementing the parameterized type Comparator<String>. Here String really is the concrete type (class) String.


Huh?
[ January 24, 2007: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Also, in your original code, what compare method are you calling in this:
?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Do you not mean to have something like this:
?
Sigrid Kajdan
Ranch Hand

Joined: Jan 14, 2007
Posts: 72
Hello Barry, that was great help!!

... you are defining a generic type with one type parameter String. That is not class String but just an identifier....
That's why you are getting (in your original code) the compiler message "The type parameter String is hiding the type String"


I'd never have found out alone (as for the other thing regarding the compare vs. compareTo, that's clear to me now).

Now, the only remaining question for me is, should I use:
class KondiComparator<T> implements Comparator<Number>
or
class KondiComparator implements Comparator<Number>

Looking at the source, I find the following method declaration:
public static <T> void sort(List<T> list, Comparator<? super T> c)

So, I suppose I ought to use
class KondiComparator<T> implements Comparator<Number>
if I want to use it for subclasses of Number... - did I get it?

And if so, should the other (simple) version
class KondiComparatorimplements Comparator<Number>
be the default, when nothing compels me to use the other?

Thanks again, Sigrid
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: generic comparator problem