Collections sorting a list made of generic class objects
Hi, I need to write a program that uses the Collections sort method in order to sort through an ArrayList with objects from a generic class.
My generic class looks like:
Code:
public class Pair<F, S> {
public F firstObject;
public S secondObject;
public Pair(F a, S b) {
firstObject = a;
secondObject = b;
}
}
I also have the Comparator classes ready.
The problem is in the main method where I'm reading the values from a text file and it looks something like:
Code:
ArrayList<Pair> list = new ArrayList<Pair>();
while(sc.hasNextLine())
list.add(new Pair(new Advisor(sc.next()), new Student(sc.next(), sc.nextDouble())));
for(Pair e: list)
System.out.println(e);
Collections.sort(list, new AdvisorPairComparator());
System.out.println("*** Sorted by advisor name ***");
for(Pair e: list)
System.out.println(e);
So I keep getting an error saying: "The method sort(List<T>, Comparator<? super T>) in the type Collections is not applicable for the arguments (ArrayList<Pair>, AdvisorPairComparator)"
Any ideas?
Thanks!
Re: Collections sorting a list made of generic class objects
How is AdvisorPairComparator defined?
Re: Collections sorting a list made of generic class objects
Code:
import java.util.Comparator;
public class AdvisorPairComparator implements Comparator<Advisor>{
@Override
public int compare(Advisor a1, Advisor a2) {
if(a1.getName().equals(a2.getName()))
return 0;
else{
int ceil = a1.getName().length()>a2.getName().length()?a2.getName().length():a1.getName().length();
for(int i=0; i < ceil; i++) {
char c1 = a1.getName().charAt(i);
char c2 = a2.getName().charAt(i);
if(c1 != c2)
return (c1<c2)?-1:1;
}
return a1.getName().length()>a2.getName().length()?1:-1;
}
}
}
Re: Collections sorting a list made of generic class objects
There are many undefined classes so the code won't compile.
If the list contains Pair objects, how can the Comparator for Advisor work with it?
Re: Collections sorting a list made of generic class objects
Ok, so I made some changes to the code and my main class looks like this:
Code:
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Driver {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception{
ArrayList<Pair> list = new ArrayList<Pair>();
Scanner sc = new Scanner(new File("data.txt"));
while(sc.hasNextLine())
list.add(new Pair<Advisor, Student>(new Advisor(sc.next()), new Student(sc.next(), sc.nextDouble())));
for(Pair<Advisor, Student> e: list)
System.out.println(e);
Collections.sort(list, new AdvisorPairComparator());
System.out.println("*** Sorted by advisor name ***");
for(Pair e: list)
System.out.println(e);
Collections.sort(list, new StudentNamePairComparator());
System.out.println("*** Sorted by student name ***");
for(Pair e: list)
System.out.println(e);
Collections.sort(list, new StudentGpaPairComparator());
System.out.println("*** Sorted by student GPA ***");
for(Pair<Advisor,Student> e: list)
System.out.println(e);
}
}
and my comparator class looks like this:
Code:
import java.util.Comparator;
public class AdvisorPairComparator implements Comparator<Pair<Advisor, Student>>
{
@SuppressWarnings("rawtypes")
public int compare(Pair a1, Pair a2) {
if(((Advisor) a1.getF()).getName().equals(((Advisor) a2.getF()).getName()))
return 0;
else{
int ceil = (((Advisor) a1.getF()).getName().length()>(((Advisor)a2.getF()).getName().length())?((Advisor) a2.getF()).getName().length():((Advisor) a1.getF()).getName().length());
for(int i=0; i < ceil; i++) {
char c1 = ((Advisor) a1.getF()).getName().charAt(i);
char c2 = ((Advisor) a2.getF()).getName().charAt(i);
if(c1 != c2)
return (c1<c2)?-1:1;
}
return ((Advisor) a1.getF()).getName().length()>((Advisor) a2.getF()).getName().length()?1:-1;
}
}
}
I keep getting an error in the main class on the Collections.sort lines saying:
The method sort(List<T>, Comparator<? super T>) in the type Collections is not applicable for the arguments (ArrayList<Pair>, AdvisorPairComparator)
Any help would be greatly appreciated.
Thanks.
Re: Collections sorting a list made of generic class objects
It will compile with these changes (plus some more)
Code:
Collections.sort(list, new AdvisorPairComparator<Pair>());
class AdvisorPairComparator<T> implements Comparator<T>{
@Override
public int compare(T a1, T a2) {