K
Kaiser S.
Hello,
here is a short class source with begining of generics. Could you please
tell me how to use generics with the comparators ?
import java.util.Comparator;
import java.util.TreeSet;
public class TestComparator {
private static abstract class A {
}
private static class B extends A {
}
private static class C extends A {
}
private static abstract class HandlerOfA {
public abstract Comparator getComparator();
}
private static class HandlerOfB extends HandlerOfA {
public static class ComparatorOfB implements Comparator<B> {
public int compare(B b1, B b2) {
return 0;
}
}
public Comparator getComparator() {
return new ComparatorOfB();
}
}
private static class HandlerOfC extends HandlerOfA {
public static class ComparatorOfC implements Comparator<C> {
public int compare(C c1, C c2) {
return 0;
}
}
public Comparator getComparator() {
return new ComparatorOfC();
}
}
public static void main(String[] args) {
HandlerOfA contA = new HandlerOfB();
HandlerOfB contB = new HandlerOfB();
HandlerOfC contC = new HandlerOfC();
TreeSet<A> treesetA = new TreeSet<A>(contA.getComparator());
treesetA.add(new B());
TreeSet<B> treesetB = new TreeSet<B>(contB.getComparator());
treesetB.add(new B());
TreeSet<C> treesetC = new TreeSet<C>(contC.getComparator());
treesetC.add(new C());
}
}
Thanks for any help
here is a short class source with begining of generics. Could you please
tell me how to use generics with the comparators ?
import java.util.Comparator;
import java.util.TreeSet;
public class TestComparator {
private static abstract class A {
}
private static class B extends A {
}
private static class C extends A {
}
private static abstract class HandlerOfA {
public abstract Comparator getComparator();
}
private static class HandlerOfB extends HandlerOfA {
public static class ComparatorOfB implements Comparator<B> {
public int compare(B b1, B b2) {
return 0;
}
}
public Comparator getComparator() {
return new ComparatorOfB();
}
}
private static class HandlerOfC extends HandlerOfA {
public static class ComparatorOfC implements Comparator<C> {
public int compare(C c1, C c2) {
return 0;
}
}
public Comparator getComparator() {
return new ComparatorOfC();
}
}
public static void main(String[] args) {
HandlerOfA contA = new HandlerOfB();
HandlerOfB contB = new HandlerOfB();
HandlerOfC contC = new HandlerOfC();
TreeSet<A> treesetA = new TreeSet<A>(contA.getComparator());
treesetA.add(new B());
TreeSet<B> treesetB = new TreeSet<B>(contB.getComparator());
treesetB.add(new B());
TreeSet<C> treesetC = new TreeSet<C>(contC.getComparator());
treesetC.add(new C());
}
}
Thanks for any help