How to write a String specific Comparator?

H

hiwa

In the below code, argument type of compare() method is declared
as String. However, compiler emits the error:

IndexSort.java:25: cannot resolve symbol
symbol: method indexOf(String)
location class java.lang.Object

Java generics is very hard to master except the top level shallowest
user layer. Could someone help writing a String specific Comparator
class properly?
---------------------------------------------
01: import java.util.*;
02:
03: public class IndexSort{
04:
05: public static void main(String[] args){
06:
07: String[] sa
08: = {"abasa", "dwapba", "oobaon", "balsa", "eglengelbao", "sveembat"};
09:
10: Arrays.sort(sa, new IndexComparator<String>("ba"));
11:
12: System.out.println(Arrays.deepToString(sa));
13: }
14:
15: static class IndexComparator<String> implements Comparator<String>{
16: String str;
17:
18: public IndexComparator(String subs){
19:
20: str = subs;
21: }
22:
23: public int compare(String o1, String o2){
24:
25: return o1.indexOf(str) - o2.indexOf(str);
26: }
27: }
28: }
----------------------------------
 
J

John McGrath

In the below code, argument type of compare() method is declared
as String.

Not exactly!
However, compiler emits the error:

IndexSort.java:25: cannot resolve symbol
symbol: method indexOf(String)
location class java.lang.Object

Java generics is very hard to master except the top level shallowest
user layer. Could someone help writing a String specific Comparator
class properly?
10: Arrays.sort(sa, new IndexComparator<String>("ba"));
15: static class IndexComparator<String> implements Comparator<String>{

In this context, "String" does not refer to the class java.lang.String.
When you place <T> after the name of the class in the class declaration,
you are creating a generic class, with the generic type parameter <T>.
You can use whatever identifier you want there: <T>, <Type> or <String>.

So you are defining a generic class IndexComparator, one that can be used
with Strings, Dates, or whatever. I do not think that is what you want.
 
H

hiwa

John McGrath said:
Not exactly!




In this context, "String" does not refer to the class java.lang.String.
When you place <T> after the name of the class in the class declaration,
you are creating a generic class, with the generic type parameter <T>.
You can use whatever identifier you want there: <T>, <Type> or <String>.

So you are defining a generic class IndexComparator, one that can be used
with Strings, Dates, or whatever. I do not think that is what you want.
Thanks John. It now works perfectly.
The result seems for me to have used only the shallowest part
of Java generics. :)
-------------------------------------------------------------
import java.util.*;

public class IndexSort{

public static void main(String[] args){

String[] sa
= {"abasa", "dwapba", "oobaon", "balsa", "eglengelbao", "sveembat"};

Arrays.sort(sa, new IndexComparator("ba"));

System.out.println(Arrays.deepToString(sa)); // JDK 1.5
}

static class IndexComparator implements Comparator<String>{
String str;

public IndexComparator(String subs){
str = subs;
}

public int compare(String o1, String o2){

return o1.indexOf(str) - o2.indexOf(str);
}
}
}
--------------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,154
Members
46,702
Latest member
LukasConde

Latest Threads

Top