File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

generic comparator problem

 
Sigrid Kajdan
Ranch Hand
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1296
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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).
 
Sigrid Kajdan
Ranch Hand
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 85
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, in your original code, what compare method are you calling in this:
?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you not mean to have something like this:
?
 
Sigrid Kajdan
Ranch Hand
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic