java 8 Lambda 表達式

list 排序java

java 8 之前寫法:
Collections.sort(list, new Comparator<CompeteCollectorResponse>() {
     @Override
     public int compare(CompeteCollectorResponse o1, CompeteCollectorResponse o2) {
       return o1.getOrder()-o2.getOrder();
     }
    });

java 8寫法:
Collections.sort(list, (CompeteCollectorResponse o1, CompeteCollectorResponse o2) -> {
    	return o1.getOrder().compareTo(o2.getOrder());
    });

或者 Collections.sort(list, (CompeteCollectorResponse a, CompeteCollectorResponse b) -> a.getOrder().compareTo(b.getOrder()));
或者 Collections.sort(list, (a, b) -> b.getOrder().compareTo(a.getOrder()));
或者list.sort((o1, o2) -> o1.getOrder().compareTo(o2.getOrder()));
相關文章
相關標籤/搜索