平常搬磚時,常常有這種狀況,根據不一樣策略對返回前端的列表元素進行各類重排序,因而有了這篇文章,學會使用Guava Orderiing。html
依然,引用下官方定義:前端
排序器(Ordering)是Guava流暢風格比較器[Comparator]的實現,它能夠用來爲構建複雜的比較器,以完成集合排序的功能。java
在使用方面,Ordering
提供了鏈式調用的支持,使得代碼變得清晰簡潔。算法
Ordering
的運行,以由Entity
對象組成的列表進行不一樣排序(筆者偷懶沒有使用Getter
和Setter
,直接public
聲明瞭成員變量。)。public class Entity {
public int status;
public String name;
public Entity(int status, String name) {
this.status = status;
this.name = name;
}
public Entity(int status) {
this.status = status;
}
@Override
public String toString() {
return "Entity{" +
"status=" + status +
", name='" + name + '\'' +
'}';
}
}
複製代碼
List<Entity> list = new ArrayList<Entity>() {{
add(new Entity(1, "h"));
add(new Entity(2, "f"));
add(new Entity(3, "a"));
add(new Entity(0, "d"));
add(new Entity(2, "b"));
}};
複製代碼
Ordering的初始化方法主要有三種,分別用於不一樣的場景。設計模式
// 整型按照大小排序
Ordering<Integer> integerOrdering = Ordering.natural();
// 日期前後排序
Ordering<Date> dateOrdering = Ordering.natural();
複製代碼
// 排序結果:[Entity{status=0, name='d'}, Entity{status=1, name='h'}, Entity{status=2, name='b'}, Entity{status=2, name='f'}, Entity{status=3, name='a'}]
Ordering<Object> ordering3 = Ordering.usingToString();
複製代碼
Comparator
初始化// 排序結果:[Entity{status=0, name='d'}, Entity{status=1, name='h'}, Entity{status=2, name='f'}, Entity{status=2, name='b'}, Entity{status=3, name='a'}]
// 按照status字段升序排序
Ordering<Entity> ordering1 = Ordering.from(Comparator.comparingInt(o -> o.status));
複製代碼
// 倒序排序
// 排序結果:[Entity{status=3, name='a'}, Entity{status=2, name='f'}, Entity{status=2, name='b'}, Entity{status=1, name='h'}, Entity{status=0, name='d'}]
Ordering<Object> ordering3 = Ordering.usingToString().reverse();
複製代碼
nullsFirst() 、nullsLast():將null值排到最前面/最後面位置。數組
compound(Comparator<? super U> secondaryComparator):合成另外一個比較器,以處理當前排序器中的相等狀況。bash
// status升序排序
Ordering<Entity> ordering1 = Ordering.from(Comparator.comparingInt(o -> o.status));
// status相等,按照name升序排序
// 排序結果:[Entity{status=0, name='d'}, Entity{status=1, name='h'}, Entity{status=2, name='b'}, Entity{status=2, name='f'}, Entity{status=3, name='a'}]
Ordering<Entity> ordering4 = ordering1.compound((o1, o2) -> StringUtils.compare(o1.name, o2.name));
複製代碼
// status升序 null的對象放在最後面
// 排序結果:[Entity{status=0, name='d'}, Entity{status=1, name='h'}, Entity{status=2, name='f'}, Entity{status=2, name='b'}, Entity{status=3, name='a'}, null]
Ordering<Entity> ordering = Ordering.natural().onResultOf(new Function<Entity, Comparable>() {
@Override
public Comparable apply(Entity entity) {
if (entity != null) {
return entity.status;
}
return -1;
}
}).nullsLast();
複製代碼
// 排序結果:[null, Entity{status=3, name='a'}, Entity{status=2, name='f'}, Entity{status=2, name='b'}]
// nullLast()致使null最大,最大的四個元素
ordering.greatestOf(list, 4)
// 排序結果:[Entity{status=0, name='d'}, Entity{status=1, name='h'}, Entity{status=2, name='f'}, Entity{status=2, name='b'}]
// 最小的四個元素
ordering.leastOf(list, 4)
複製代碼
// 結果:Entity{status=0, name='d'}
ordering.min(list)
// 還支持N個對象的比較
// 結果:Entity{status=1, name='y'}
ordering.min(new Entity(1, "y"), new Entity(2, "x"))
複製代碼
public <E extends T> List<E> sortedCopy(Iterable<E> iterable) {
// 轉換成了對象數組
E[] array = (Object[])Iterables.toArray(iterable);
// Ordering繼承Comparator,使用Ordering排序
Arrays.sort(array, this);
// 最後複製到新建列表中
return Lists.newArrayList(Arrays.asList(array));
}
複製代碼
Ordering
將排序結果做爲新的列表對象,原有列表沒有改動。// 數據:
List<Entity> list = new ArrayList<Entity>() {{
add(new Entity(1, "h"));
add(new Entity(2, "f"));
add(new Entity(3, "a"));
add(new Entity(0, "d"));
add(new Entity(2, "b"));
add(null);
}};
// 結果:[Entity{status=0, name='d'}, Entity{status=1, name='h'}, Entity{status=2, name='f'}, Entity{status=2, name='b'}, Entity{status=3, name='a'}, null]
[Entity{status=1, name='h'}, Entity{status=2, name='f'}, Entity{status=3, name='a'}, Entity{status=0, name='d'}, Entity{status=2, name='b'}, null]
System.out.println(ordering.sortedCopy(list));
System.out.println(list);
複製代碼
public interface IEntityStrategy {
List<Entity> sort(List<Entity> list);
}
複製代碼
public class StatusDESCStrategy implements IEntityStrategy {
private static Ordering<Entity> ordering = Ordering.natural().onResultOf(new Function<Entity, Comparable>() {
@Override
public Comparable apply(Entity entity) {
if (entity != null) {
return entity.status;
}
return -1;
}
}).nullsLast();
@Override
public List<Entity> sort(List<Entity> list) {
return ordering.sortedCopy(list);
}
}
複製代碼
public class NameASCStrategy implements IEntityStrategy {
private static Ordering<Entity> ordering = Ordering.from((o1, o2) -> StringUtils.compare(o1.name, o2.name));
@Override
public List<Entity> sort(List<Entity> list) {
return ordering.sortedCopy(list);
}
}
複製代碼
public class Test {
public static void main(String[] args) {
List<Entity> list = new ArrayList<Entity>() {{
add(new Entity(1, "h"));
add(new Entity(2, "f"));
add(new Entity(3, "a"));
add(new Entity(0, "d"));
add(new Entity(2, "b"));
add(null);
}};
Context context = new Context(new StatusDESCStrategy());
System.out.println("status字段降序排序");
// 結果:[Entity{status=3, name='a'}, Entity{status=2, name='f'}, Entity{status=2, name='b'}, Entity{status=1, name='h'}, Entity{status=0, name='d'}, null]
System.out.println(context.sortByStrategy(list));
// 結果:[Entity{status=3, name='a'}, Entity{status=2, name='b'}, Entity{status=0, name='d'}, Entity{status=2, name='f'}, Entity{status=1, name='h'}, null]
Context context1 = new Context(new NameASCStrategy());
System.out.println("name字段升序排序");
System.out.println(context1.sortByStrategy(list));
}
}
複製代碼
Context
,直接上策略類處理。ifeve.com/google-guav… 設計模式.pdf www.runoob.com/design-patt…app