diff --git "a/data/3.Java\351\253\230\351\230\266/4.\346\224\266\351\233\206\345\231\250/config.json" "b/data/3.Java\351\253\230\351\230\266/4.\346\224\266\351\233\206\345\231\250/config.json" index 015f92dca3f02b18c22b9ead89e2c672f7e7a4e8..6edd4d8df9cbb97532fb18fbf9f9dcb2c44e49c1 100644 --- "a/data/3.Java\351\253\230\351\230\266/4.\346\224\266\351\233\206\345\231\250/config.json" +++ "b/data/3.Java\351\253\230\351\230\266/4.\346\224\266\351\233\206\345\231\250/config.json" @@ -2,6 +2,5 @@ "node_id": "java-0f1fc49b2e1c41368403ca2239810920", "keywords": [], "children": [], - "export": [], - "title": "收集器简介" + "export": ["to_list.json"] } \ No newline at end of file diff --git "a/data/3.Java\351\253\230\351\230\266/4.\346\224\266\351\233\206\345\231\250/to_list.json" "b/data/3.Java\351\253\230\351\230\266/4.\346\224\266\351\233\206\345\231\250/to_list.json" new file mode 100644 index 0000000000000000000000000000000000000000..53268b0021a3a48e9cb26c04eadd953b24039537 --- /dev/null +++ "b/data/3.Java\351\253\230\351\230\266/4.\346\224\266\351\233\206\345\231\250/to_list.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "刘鑫", + "source": "to_list.md", + "notebook_enable": false +} \ No newline at end of file diff --git "a/data/3.Java\351\253\230\351\230\266/4.\346\224\266\351\233\206\345\231\250/to_list.md" "b/data/3.Java\351\253\230\351\230\266/4.\346\224\266\351\233\206\345\231\250/to_list.md" new file mode 100644 index 0000000000000000000000000000000000000000..924b06db9433c0053e5786ef5bf07e2d49b45940 --- /dev/null +++ "b/data/3.Java\351\253\230\351\230\266/4.\346\224\266\351\233\206\345\231\250/to_list.md" @@ -0,0 +1,35 @@ +# Stream To List + +下面的选项尝试从一个整数 Stream 对象中构造 List。那么不正确的是: + +## 答案 + +```java +return new ArrayList<>(stream); +``` + +## 选项 + +### 推荐做法 + +```java +return stream.collect(Collectors.toList()); +``` + +### 朴素做法,不推荐但可用 + +```java +List result = new ArrayList<>(); +stream.foreach(x -> { + result.add(x); +}); +return result; +``` + +### 方法引用 + +```java +List result = new ArrayList<>(); +stream.foreach(result::add); +``` + diff --git "a/data/3.Java\351\253\230\351\230\266/4.\346\224\266\351\233\206\345\231\250/to_set.json" "b/data/3.Java\351\253\230\351\230\266/4.\346\224\266\351\233\206\345\231\250/to_set.json" new file mode 100644 index 0000000000000000000000000000000000000000..e53c216c17a4e8d88123a1fe619a79f616d9cb49 --- /dev/null +++ "b/data/3.Java\351\253\230\351\230\266/4.\346\224\266\351\233\206\345\231\250/to_set.json" @@ -0,0 +1,6 @@ +{ + "type": "code_options", + "author": "刘鑫", + "source": "to_set.md", + "notebook_enable": false +} \ No newline at end of file diff --git "a/data/3.Java\351\253\230\351\230\266/4.\346\224\266\351\233\206\345\231\250/to_set.md" "b/data/3.Java\351\253\230\351\230\266/4.\346\224\266\351\233\206\345\231\250/to_set.md" new file mode 100644 index 0000000000000000000000000000000000000000..abab24714dc5934f40f479135f89d3637c0cbeba --- /dev/null +++ "b/data/3.Java\351\253\230\351\230\266/4.\346\224\266\351\233\206\345\231\250/to_set.md" @@ -0,0 +1,39 @@ +# Stream To Set + +下面的选项尝试从一个整数 Stream 对象中构造 List。那么不正确的是: + +## 答案 + +```java +return new HashSet<>(stream); +``` + +## 选项 + +### 推荐做法 + +```java +return stream.collect(Collectors.toSet()); +``` + +### 朴素做法,不推荐但可用 + +```java +List result = new ArrayList<>(); + stream.foreach(x -> { + result.add(x); + }); + return result; +``` + +### 方法引用 + +```java +Set result = new HashSet<>(); +stream.foreach(result::add); +``` + + + + +