public static void main(String[] args) {
// Ordering.from(Comparator.comparing())
List<String> list = Lists.newArrayList("10","2","5");
String min = Ordering.from(Comparator.comparingInt((String s) -> Integer.valueOf(s))).min(list);
System.out.println(min);
// 對集合中元素調用Function,再按返回值用當前排序器排序。
Ordering<FastCode> ordering = Ordering.natural().nullsFirst().onResultOf(new Function<FastCode, String>() {
public String apply(FastCode foo) {
return foo.getMeaning();
}
});
FastCode fastCode = new FastCode();
fastCode.setMeaning("11");
FastCode fastCode2 = new FastCode();
fastCode2.setMeaning("12");
FastCode fastCode3 = new FastCode();
fastCode3.setMeaning("13");
List<FastCode> fastCodeList = Lists.newArrayList(fastCode2,fastCode,fastCode3);
Collections.sort(fastCodeList,ordering);
for (FastCode code : fastCodeList) {
System.out.print(code.getMeaning()+" ");
}
}
Guava Ordering 使用
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.