场景
在使用collection.stream场景时,有时候需要对元素进行执行规则排序,有时候又需要逆序,虽然Comparator提供了排序以及逆序,但是逆序时不仅需要继续调用reversed()方法,还得是准确类型才能逆.
逆序工具
- 工具类
package com.littlehow.utils;
import java.util.Collections;
import java.util.Comparator;
import java.util.Objects;
import java.util.function.Function;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
/**
* lambda表达式排序时便于倒排
*/
public class Comparators {
/**
* 根据指定值倒排(int类型)
* @see Comparator#comparingInt(ToIntFunction) .reversed()
* @param key
* @param <T>
* @return
*/
public static <T> Comparator<T> reverseIntSort(ToIntFunction<? super T> key) {
Objects.requireNonNull(key);
return (v1, v2) -> Integer.compare(key.applyAsInt(v2), key.applyAsInt(v1));
}
/**
* 根据指定值倒排(long类型)
* @see Comparator#comparingLong(ToLongFunction) .reversed()
* @param key
* @param <T>
* @return
*/
public static <T> Comparator<T> reverseLongSort(ToLongFunction<? super T> key) {
Objects.requireNonNull(key);
return (v1, v2) -> Long.compare(key.applyAsLong(v2), key.applyAsLong(v1));
}
/**
* 根据指定值倒排(double类型)
* @see Comparator#comparingDouble(ToDoubleFunction) .reversed()
* @param key
* @param <T>
* @return
*/
public static <T> Comparator<T> reverseDoubleSort(ToDoubleFunction<? super T> key) {
Objects.requireNonNull(key);
return (v1, v2) -> Double.compare(key.applyAsDouble(v2), key.applyAsDouble(v1));
}
/**
* 对可排序的类型进行倒排, 兼容reverseIntSort/reverseLongSort/reverseDoubleSort
* @see Comparator#comparing(Function) .reversed()
* @param key
* @param <T>
* @param <U>
* @return
*/
public static <T, U extends Comparable<? super U>> Comparator<T> reverseSort(Function<? super T, ? extends U> key) {
Objects.requireNonNull(key);
return (v1, v2) -> key.apply(v2).compareTo(key.apply(v1));
}
/**
* 对自身可排序的进行逆序
* @see Comparator#reverseOrder()
* @param <T>
* @return
*/
public static <T extends Comparable<? super T>> Comparator<T> reverseSort() {
return Collections.reverseOrder();
}
}
- 测试类
import com.littlehow.utils.Comparators;
import lombok.Builder;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
public class ComparatorTest {
public static void main(String[] args) {
//对int类型进行排序
List<A> as = new ArrayList<>();
as.add(A.builder().v(3).build());
as.add(A.builder().v(1).build());
as.add(A.builder().v(4).build());
as.add(A.builder().v(2).build());
//对as进行正向排序
System.out.println(as.stream().sorted(Comparator.comparingInt(P::getV)).collect(Collectors.toList()));
//原始提供的逆序方式,不能使用父类P::getV()
System.out.println(as.stream().sorted(Comparator.comparingInt(A::getV).reversed()).collect(Collectors.toList()));
//对as进行逆向排序
System.out.println(as.stream().sorted(Comparators.reverseIntSort(P::getV)).collect(Collectors.toList()));
//原始顺序(不变)
System.out.println(as);
//对long类型进行排序
List<B> bs = new ArrayList<>();
bs.add(B.builder().v(32L).build());
bs.add(B.builder().v(19L).build());
bs.add(B.builder().v(44L).build());
bs.add(B.builder().v(26L).build());
//对bs进行正向排序
System.out.println(bs.stream().sorted(Comparator.comparingLong(P::getV)).collect(Collectors.toList()));
//对bs进行逆向排序
System.out.println(bs.stream().sorted(Comparators.reverseLongSort(P::getV)).collect(Collectors.toList()));
//原始顺序(不变)
System.out.println(bs);
//对double类型进行排序
List<C> cs = new ArrayList<>();
cs.add(C.builder().v(1.1).build());
cs.add(C.builder().v(1.3).build());
cs.add(C.builder().v(1.5).build());
cs.add(C.builder().v(1.2).build());
//对cs进行正向排序
System.out.println(cs.stream().sorted(Comparator.comparingDouble(P::getV)).collect(Collectors.toList()));
//对cs进行逆向排序
System.out.println(cs.stream().sorted(Comparators.reverseDoubleSort(P::getV)).collect(Collectors.toList()));
//原始顺序(不变)
System.out.println(cs);
//对可排序的类型进行排序
List<D> ds = new ArrayList<>();
ds.add(D.builder().v("2").build());
ds.add(D.builder().v("1").build());
ds.add(D.builder().v("4").build());
ds.add(D.builder().v("3").build());
ds.add(D.builder().v("5").build());
//对ds进行正向排序
System.out.println(ds.stream().sorted(Comparator.comparing(P::getV)).collect(Collectors.toList()));
//原始提供的逆序方式,不能使用父类P::getV()
System.out.println(ds.stream().sorted(Comparator.comparing(D::getV).reversed()).collect(Collectors.toList()));
//对ds进行逆向排序
System.out.println(ds.stream().sorted(Comparators.reverseSort(P::getV)).collect(Collectors.toList()));
//原始顺序(不变)
System.out.println(ds);
//自身就是可排序的排序
List<String> ss = new ArrayList<>();
ss.add("8");
ss.add("9");
ss.add("5");
ss.add("4");
ss.add("7");
ss.add("6");
//对ss进行正向排序
System.out.println(ss.stream().sorted().collect(Collectors.toList()));
//对ss进行反向排序
System.out.println(ss.stream().sorted(Comparator.reverseOrder()).collect(Collectors.toList()));
//原始顺序(不变)
System.out.println(ss);
}
}
class P<T> {
protected T v;
protected T getV() {
return v;
}
public String toString() {
return Objects.toString(v);
}
}
class A extends P<Integer> {
@Builder
A(Integer v) {
this.v = v;
}
}
class B extends P<Long> {
@Builder
B(Long v) {
this.v = v;
}
}
class C extends P<Double> {
@Builder
C(Double v) {
this.v = v;
}
}
class D extends P<String> {
@Builder
D(String v) {
this.v = v;
}
}