通常我将Comparator
定义为 -
Comparator<Obj> comparator= new Comparator<Obj>() {
public int compare(Obj o1, Obj o2) {
//implementation...
}
};
当Comaprator
是类memeber的一部分时,它在代码格式上看起来不太好看。有没有办法将Comparator
定义为Comparator<Obj> comparator= new Comparator<Obj>()
并在另一部分实施他的compare
?
答案 0 :(得分:1)
只需定义一个实现class
界面的Comparator
,并使用compare
在该类中实施comparison logic
方法: -
public class ObjComparator implements Comparator<Obj> {
public int compare(Obj o1, Obj o2) {
//implementation
}
}
然后,在您想要使用Comparator
的地方,请使用此类的实例。例如,在Arrays.sort
中: -
Obj[] yourArray = new Obj[10];
Arrays.sort(yourArray, new ObjComparator());
答案 1 :(得分:1)
您可以拥有一个实现Comparator
的单独类:
public class MyComapartor implements Comparator{
public int compare(Obj o1, Obj o2)
{ //implementation...
}
}
然后在课堂上使用它:
public class MyClass{
MyComapartor comp = new MyComapartor();
}
答案 2 :(得分:1)
试试这段代码......
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
class HDTV {
private int size;
private String brand;
public HDTV(int size, String brand) {
this.size = size;
this.brand = brand;
}
public int getSize() {
return size;
}
public void setSize(int size) {
this.size = size;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
}
class SizeComparator implements Comparator<HDTV> {
@Override
public int compare(HDTV tv1, HDTV tv2) {
int tv1Size = tv1.getSize();
int tv2Size = tv2.getSize();
if (tv1Size > tv2Size) {
return 1;
} else if (tv1Size < tv2Size) {
return -1;
} else {
return 0;
}
}
}
public class Main {
public static void main(String[] args) {
HDTV tv1 = new HDTV(55, "Samsung");
HDTV tv2 = new HDTV(60, "Sony");
HDTV tv3 = new HDTV(42, "Panasonic");
ArrayList<HDTV> al = new ArrayList<HDTV>();
al.add(tv1);
al.add(tv2);
al.add(tv3);
Collections.sort(al, new SizeComparator());
for (HDTV a : al) {
System.out.println(a.getBrand());
}
}
}
答案 3 :(得分:0)
试试这段代码..............
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
class Car {
private String name;
private String brand;
private double cost;
public Car(String name, String brand, double cost) {
this.name = name;
this.brand = brand;
this.cost = cost;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public double getCost() {
return cost;
}
public void setCost(double cost) {
this.cost = cost;
}
public String toString() {
return getName();
}
}
public class Hog {
ArrayList<Car> cars = new ArrayList<Car>();
public void setIt() {
cars.add(new Car("Padmini", "Fiat", 100008.00));
cars.add(new Car("XYlo", "Mahindra", 100000.00));
cars.add(new Car("Swift", "Maruti", 200000.00));
}
public void sortIt() {
Collections.sort(cars, new NameComparator());
System.out.println(cars);
Collections.sort(cars, new BrandComparator());
System.out.println(cars);
Collections.sort(cars, new CostComparator());
System.out.println(cars);
}
class NameComparator implements Comparator<Car> {
public int compare(Car c1, Car c2) {
return c1.getName().compareTo(c2.getName());
}
}
class BrandComparator implements Comparator<Car> {
public int compare(Car c1, Car c2) {
return c1.getBrand().compareTo(c2.getBrand());
}
}
class CostComparator implements Comparator<Car> {
public int compare(Car c1, Car c2) {
return new Double(c1.getCost()).compareTo(new Double(c2.getCost()));
}
}
public static void main(String[] args) {
Hog h = new Hog();
h.setIt();
h.sortIt();
}
}
答案 4 :(得分:0)
您是否尝试使用嵌套类,例如:
public class Sample {
public void method() {
ObjComparator comparator = new ObjComparator();
}
public static class ObjComparator implements Comparator {
public int compare(Obj o1, Obj o2) {
}
}
}
这是您寻求的解决方案吗?