This commit is contained in:
xiangwb
2018-02-10 17:06:50 +08:00
parent 1a3c7ef5d2
commit f87bb2bdb0
2 changed files with 4 additions and 8 deletions
@@ -91,7 +91,7 @@ public class CollectionDemo {
boolean isEmpty = list.isEmpty();// 是否为空
boolean contains = list.contains(Integer.valueOf(1));// 是否包含元素
// l.clear();//清除集合元素
Set<Integer> s = new HashSet<Integer>();// 无序
Set<Integer> s = new HashSet<>();// 无序
s.add(111);
s.add(222);
s.add(333);
@@ -87,7 +87,7 @@ public class LambdaDemo {
}
/**
* 遍历集合,集合里数据还要进行复杂操作,导致速度很慢
* 遍历集合,集合里数据还要进行复杂操作,导致速度很慢,用以下操作
*/
public List<Integer> supplyAsync() {
List<Integer> list = new ArrayList<>();
@@ -96,12 +96,12 @@ public class LambdaDemo {
}
int size = list.size();
CompletableFuture[] futures = new CompletableFuture[size];
List<Integer> finalList = new ArrayList<>();
List<Integer> finalList = new ArrayList<>(size);
for (int i = 0; i < size; i++) {
finalList.add(null);//必须有这步,否则会下标越界
Integer integer = list.get(i);
final int pos = i;
futures[i] = CompletableFuture.supplyAsync(() -> finalList.set(pos, integer),taskExecutor);//按原来顺序存
futures[i] = CompletableFuture.supplyAsync(() -> finalList.set(pos, integer), taskExecutor);//按原来顺序存
}
CompletableFuture.allOf(futures).join();//线程等待,效果等同于get(),会拋出CompletionException
// CompletableFuture<Void> completableFuture = CompletableFuture.allOf(futures);
@@ -146,10 +146,6 @@ public class LambdaDemo {
return Collections.EMPTY_LIST;
}
private JSONObject setData(JSONObject object) {
return object;
}
private String isAdmin(String id) {
return "Y";
}