class Foo { private String id; public Foo(String id) { this.id = id; } public String getId() { return id; } @Override public String toString() { return id; } } List<Foo> list = Arrays.asList(new Foo("1"), new Foo("2"), new Foo("3"), new Foo("4")); List<String> keys = Arrays.asList("2","1","3","4"); // before Java 8 List<Foo> result1 = new ArrayList<>(list); Collections.sort(result1, new Comparator() { @Override public int compare(Foo o1, Foo o2) { return keys.indexOf(o1.getId()) < keys.indexOf(o2.getId()) ? -1 : 1; } }); // after Java 8 List<Foo> result2 = list.stream() .sorted(Comparator.comparing(e -> keys.indexOf(e.getId()))) .collect(Collectors.toList()); System.out.println(result1); // [2, 1, 3, 4] System.out.println(result2); // [2, 1, 3, 4]
2018年5月31日 星期四
[Java] 使用另一個列表進行排序
訂閱:
張貼留言 (Atom)
[Java] Invalid HTTP method: PATCH
最近系統需要使用 Netty4,所以把衝突的 Netty3 拆掉,然後就出現了例外。 pom.xml <dependency> <groupId>com.ning</groupId> <artifactId>as...
-
有時候會發生沒有資料可以塞入 ListView ,此時需要一個 TextView 頂替: <ListView android:id="@+id/listView" android:layout_width="match_p...
-
import java.util.EnumSet; public enum Status { PENDING("pending"), ACTIVE("active"); public static final ...
-
AWS Lambda 有提供 proxy framework 掛上 jersey。 首先加上依賴包, // build.gradle dependencies { compile ( 'com.amazonaws.serverless:aws...
沒有留言:
張貼留言