strategy 策略模式
public class Cat {
private Integer height;
private Integer weight;
}
現(xiàn)有一個List<Cat>屠升,要對它進行排序地回。
List<Cat> list = new ArrayList<>();
list.add(new Cat(1,5));
list.add(new Cat(5,3));
list.add(new Cat(2,4));
用Comparator加泛型新增一個策略
Comparator<Cat> heightNullFisrtThenWeight = Comparator.nullsFirst(Comparator.comparing(Cat::getHeight)).thenComparing(Cat::getWeight);
list.sort(heightNullFisrtThenWeight);
針對不同的設計有不同的排序策略,對它的排序規(guī)則進行設計似芝;