内部 Class 与来自比较器 JAVA 的比较方法
Inner Class with Compare Method from Comparator JAVA
为了某个项目,我必须按地区和名称订购一些图形。首先,它们按面积从大到小排序,如果它们重合,则按字母顺序排序。在名为 RectangularRegion
的 class 中,我有一个以下类型的内部 class:
//Inner Class
static class RectangularRegionComparator implements Comparator<RectangularRegion>{
@Override
public int compare(RectangularRegiono1, RectangularRegiono2) {
//Return a positive integer if the area of o1 is higher than o2 [we want o1 to go before o2 (o1 <o2)], a negative is o1 and less than o2 and 0 are equal, then we will sort by name from A to Z
if ((o1.Area() - o2.Area()) != 0){
return o1.Area() - o2.Area();
}
return o1.name.compareTo(o2.name);
}
}
我想从另一个class中排序一组RectangularRegion
怎么办?我认为最好的方法如下,但它失败了 (The method sort(List<T>, Comparator<? super T>) in the type Collections is not applicable for the arguments (Collection<RectangularRegion>, RectangularRegion.RectangularRegionComparator)
):
public void ordenarRegionesXAreaAsc(){
Collections.sort(rex, new RectangularRegion.RectangularRegionComparator());
}
哪里private Collection<RegionRectangular> rex= new ArrayList<RectangularRegion>();
我做错了什么?
您无法对 Collection
进行排序,因为此界面不会对其元素强加顺序。
改为声明
private List<RegionRectangular> rex= new ArrayList<RectangularRegion>();
旁注
从 Java 1.8 开始,Comparator
class 有一些方便的方法,允许您设置一个 Comparator
实例而无需实现接口。
例如你可以这样做
// instead of class RectangualarRegionComparator
static final Comparator<RectangularRegion> NATURAL_ORDER =
Comparator.comparing(RectangularRegion::Area)
.thenComparing(region -> region.name);
// or to avoid auto-boxing
static final Comparator<RectangularRegion> NATURAL_ORDER =
Comparator.comparing(region -> Integer.valueOf(region.Area()))
.thenComparing(region -> region.name);
和
public void ordenarRegionesXAreaAsc() {
Collections.sort(rex, RectangularRegion.NATURAL_ORDER);
}
为了某个项目,我必须按地区和名称订购一些图形。首先,它们按面积从大到小排序,如果它们重合,则按字母顺序排序。在名为 RectangularRegion
的 class 中,我有一个以下类型的内部 class:
//Inner Class
static class RectangularRegionComparator implements Comparator<RectangularRegion>{
@Override
public int compare(RectangularRegiono1, RectangularRegiono2) {
//Return a positive integer if the area of o1 is higher than o2 [we want o1 to go before o2 (o1 <o2)], a negative is o1 and less than o2 and 0 are equal, then we will sort by name from A to Z
if ((o1.Area() - o2.Area()) != 0){
return o1.Area() - o2.Area();
}
return o1.name.compareTo(o2.name);
}
}
我想从另一个class中排序一组RectangularRegion
怎么办?我认为最好的方法如下,但它失败了 (The method sort(List<T>, Comparator<? super T>) in the type Collections is not applicable for the arguments (Collection<RectangularRegion>, RectangularRegion.RectangularRegionComparator)
):
public void ordenarRegionesXAreaAsc(){
Collections.sort(rex, new RectangularRegion.RectangularRegionComparator());
}
哪里private Collection<RegionRectangular> rex= new ArrayList<RectangularRegion>();
我做错了什么?
您无法对 Collection
进行排序,因为此界面不会对其元素强加顺序。
改为声明
private List<RegionRectangular> rex= new ArrayList<RectangularRegion>();
旁注
从 Java 1.8 开始,Comparator
class 有一些方便的方法,允许您设置一个 Comparator
实例而无需实现接口。
例如你可以这样做
// instead of class RectangualarRegionComparator
static final Comparator<RectangularRegion> NATURAL_ORDER =
Comparator.comparing(RectangularRegion::Area)
.thenComparing(region -> region.name);
// or to avoid auto-boxing
static final Comparator<RectangularRegion> NATURAL_ORDER =
Comparator.comparing(region -> Integer.valueOf(region.Area()))
.thenComparing(region -> region.name);
和
public void ordenarRegionesXAreaAsc() {
Collections.sort(rex, RectangularRegion.NATURAL_ORDER);
}