如何在 Java 中的比较器 class 中使用 lambda 表达式
How can I use lambda expressions in a Comparator class in Java
我是 Java 的新手,现在我必须创建一些比较器 class。
在这个 Whosebug 页面上,我找到了一些关于使用 lambda 表达式的非常有用的信息。 How to compare objects by multiple fields
这让我想像这样创建 Compartor class:
public class WidthComparator implements Comparator{
@Override
public int compare(Object t, Object t1) {
Foto foto1 = (Foto)t;
Foto foto2 = (Foto)t1;
return Comparator.comparing(Foto::getWidth)
.thenComparing(Foto::getHeight)
.thenComparingInt(Foto::getName);
}
}
}
所以当我有一个名为 fotosCollection 的集合时,我希望能够这样做:
fotosCollection.sort(new HoogteComparator());
这显然行不通,但我怎样才能让它起作用?
Ps。我必须使用比较器 class.
您可以试试这个old-style方法:
public class WidthComparator implements Comparator{
@Override
public int compare(Object t, Object t1) {
Foto foto1 = (Foto)t;
Foto foto2 = (Foto)t1;
// width asc order
if(foto1.getWidth() != foto2.getWidth())
return foto1.getWidth() - foto2.getWidth();
// height asc order
if(foto1.getHeight() != foto2.getHeight())
return foto1.getHeight() - foto2.getHeight();
// name asc order
return foto1.getName().compareTo(foto2.getName());
}
}
Comparator.comapring
returns一个Comparator
- 你可以直接使用它:
// Define a "constant" comparator
private static final Comparator<Foo> HOOGTE_COMPARATOR =
Comparator.comparing(Foto::getWidth)
.thenComparing(Foto::getHeight)
.thenComparingInt(Foto::getName);
// Use it elsewhere in your code
fotosCollection.sort(HOOGTE_COMPARATOR);
如果出于某种原因你真的不希望比较器类型是匿名的,你可以这样做:
public class WidthComparator implements Comparator<Foto>{
private final static Comparator<Foto> FOTO_COMPARATOR = Comparator.comparing(Foto::getWidth)
.thenComparing(Foto::getHeight)
.thenComparingInt(Foto::getName);
@Override
public int compare(Foto foto1, Foto foto2) {
return FOTO_COMPARATOR.compare(foto1, foto2);
}
}
我也会考虑避免使用 rawtype 并实现 Comparator<Foto>
,就像我上面所做的那样。
我是 Java 的新手,现在我必须创建一些比较器 class。
在这个 Whosebug 页面上,我找到了一些关于使用 lambda 表达式的非常有用的信息。 How to compare objects by multiple fields
这让我想像这样创建 Compartor class:
public class WidthComparator implements Comparator{
@Override
public int compare(Object t, Object t1) {
Foto foto1 = (Foto)t;
Foto foto2 = (Foto)t1;
return Comparator.comparing(Foto::getWidth)
.thenComparing(Foto::getHeight)
.thenComparingInt(Foto::getName);
}
}
}
所以当我有一个名为 fotosCollection 的集合时,我希望能够这样做:
fotosCollection.sort(new HoogteComparator());
这显然行不通,但我怎样才能让它起作用?
Ps。我必须使用比较器 class.
您可以试试这个old-style方法:
public class WidthComparator implements Comparator{
@Override
public int compare(Object t, Object t1) {
Foto foto1 = (Foto)t;
Foto foto2 = (Foto)t1;
// width asc order
if(foto1.getWidth() != foto2.getWidth())
return foto1.getWidth() - foto2.getWidth();
// height asc order
if(foto1.getHeight() != foto2.getHeight())
return foto1.getHeight() - foto2.getHeight();
// name asc order
return foto1.getName().compareTo(foto2.getName());
}
}
Comparator.comapring
returns一个Comparator
- 你可以直接使用它:
// Define a "constant" comparator
private static final Comparator<Foo> HOOGTE_COMPARATOR =
Comparator.comparing(Foto::getWidth)
.thenComparing(Foto::getHeight)
.thenComparingInt(Foto::getName);
// Use it elsewhere in your code
fotosCollection.sort(HOOGTE_COMPARATOR);
如果出于某种原因你真的不希望比较器类型是匿名的,你可以这样做:
public class WidthComparator implements Comparator<Foto>{
private final static Comparator<Foto> FOTO_COMPARATOR = Comparator.comparing(Foto::getWidth)
.thenComparing(Foto::getHeight)
.thenComparingInt(Foto::getName);
@Override
public int compare(Foto foto1, Foto foto2) {
return FOTO_COMPARATOR.compare(foto1, foto2);
}
}
我也会考虑避免使用 rawtype 并实现 Comparator<Foto>
,就像我上面所做的那样。