我试图弄清楚stream.of()和之间的差异在哪里stream(),我遇到了这个。谁能向我解释为什么第一个不起作用?
//Given: List<Integer> transactions
transactions.add(1);
transactions.add(2);
//This won't compile,
Stream.of(transactions).filter(x->x<3).collect(Collectors.toList());
// while this one does fine:
transactions.stream().filter(x->x<3).collect(Collectors.toList());
Run Code Online (Sandbox Code Playgroud)
Stream.of创建给定参数List.stream的流并将您的列表转换为流。
鉴于:
List<Integer> myList = Arrays.asList(1, 2, 3);
Run Code Online (Sandbox Code Playgroud)
myList.stream()返回Stream<Integer>内容为 {1, 2, 3}
Stream.of(myList)返回一个Stream<List<Integer>>内容 { myList }
| 归档时间: |
|
| 查看次数: |
68 次 |
| 最近记录: |