提交 a1cac26d 编写于 作者: M Mars Liu

lambda and high order

上级 65e6fb7c
{
"node_id": "java-3a05a505cd514579a52321f0c945d577",
"keywords": [],
"children": [],
"export": [],
"title": "行为参数化"
}
\ No newline at end of file
{
"node_id": "java-b121d453fc544e188b9ad50476c22f59",
"keywords": [],
"children": [
],
"export": ["sam.json"]
}
\ No newline at end of file
{
"node_id": "java-db33d9c7f2004cdbbc058254fe536ace",
"keywords": [],
"children": [
],
"export": ["double.json"],
"title": "真实的例子"
}
\ No newline at end of file
......@@ -2,6 +2,9 @@
"node_id": "java-fb711fb0dfbb40db974476ea2d9faf9d",
"keywords": [],
"children": [],
"export": [],
"export": [
"map.json",
"sum.json"
],
"title": "通过行为参数化传递代码"
}
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"source": "double.md",
"notebook_enable": false
}
\ No newline at end of file
# 过滤
Stream API支持 filter 方法,接受一段过滤代码,使得调用者可以传入过滤规则,返回过滤后的新的 Stream,那么下列调用代码不正确的是:
## 答案
```java
IntStream stream=IntStream.range(0,100);
return stream.filter(x%2==0);
```
## 选项
### 标准的 lambda 调用
```java
IntStream stream=IntStream.range(0,100);
return stream.filter(x->x%2==0);
```
### lambda 的完整形式
```java
IntStream stream=IntStream.range(0,100);
return stream.filter(x->{
return x%2==0;
});
```
### 对象方法引用
```java
IntStream stream=IntStream.range(0,100);
return stream.filter(this::isEven);
```
### 类型方法引用
```java
IntStream stream=IntStream.range(0,100);
return stream.filter(App::isEven);
```
### 可以显式使用完整的函数对象定义
```java
IntStream stream=IntStream.range(0,100);
IntPredicate filter = new IntPredicate() {
@Override
public boolean test(int value) {
return value % 2 == 0;
}
};
return stream.filter(filter);
```
\ No newline at end of file
# 翻倍操作
下面哪个选项可以从 `Stream<Integer> stream` 构造一个新的 List<Ineger> 对象,使其包含stream中每个元素翻倍后的结果:
## template
```java
import java.util.stream.Stream;
import java.util.stream.IntStream;
public class App{
public void main(String[] args){
Stream<Integer> stream = IntStream.range(0, 100).boxed();
$code
for(var item : result){
System.out.println(item);
}
}
}
```
## 答案
```java
List<Integer> result = stream.map(x->x*2).collect(Collectors.toList());
```
## 选项
### 没有变换为 List
```java
List<Integer> result = stream.map(x->x*2);
```
### lambda 格式错误
```java
List<Integer> result = stream.map(* 2).collect(Collectors.toList());
```
### 错误的使用了 foreach
```java
List<Integer> result = stream.foreach(item -> {
return item * 2
});
```
{
"type": "code_options",
"author": "刘鑫",
"source": "sum.md",
"notebook_enable": false
}
\ No newline at end of file
# 求和
下面代码对整数流求和,其中有错的是
## 答案
```java
Stream<Integer> stream = IntStream.range(0,100).boxed();
return stream.mapToInt(x -> x);
```
## 选项
### 标准库方法
```java
Stream<Integer> stream = IntStream.range(0,100).boxed();
return stream.sum();
```
### 显式调用 simmingInt 方法,可以定义一些复杂的处理逻辑
```java
var collector = Collectors.<Integer>summingInt(x->x);
return stream.collect(collector);
```
### 也可以封装到一个朴素的方法中,作为隐式简写
```java
var collector = Collectors.<Integer>summingInt(this::identity);
return stream.collect(collector);
```
### 也可以封装到一个朴素的方法中,作为隐式简写
```java
var collector = Collectors.<Integer>summingInt(App::identity);
return stream.collect(collector);
```
\ No newline at end of file
......@@ -2,6 +2,9 @@
"node_id": "java-a3b60740abe74bb8a6c16dec381d31c4",
"keywords": [],
"children": [],
"export": [],
"export": [
"lambda.json",
"sam.json"
],
"title": "Lambda表达式"
}
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"source": "handler.md",
"notebook_enable": false
}
\ No newline at end of file
# 函数式的参数
下面这个代码所定义的函数,用哪一种代码调用会出错?
```java
import java.util.function.Function;
public class App {
public <T, R> product(Function<T, R> consumer) {
// 这里有一段代码生成了 T value 变量
return consumer(value);
}
// 省略其它代码
}
```
## 答案
```java
Predicate<Float> checker = value -> {
return this.exp(value) < 1;
}
```
## 选项
### lambda 形式
```java
app.product(value -> {
return sigmod(value);
});
```
### 对象方法
```java
app.product(this::sigmod)
```
### 类型方法
```java
app.product(App::sigmod)
```
### Function 对象
```java
Function<Float, Float> checker = value -> {
if(this.exp(value) < 1){
retrun 1/value;
} else {
return sigmod(value);
}
}
```
\ No newline at end of file
{
"type": "code_options",
"author": "刘鑫",
"source": "lambda.md",
"notebook_enable": false
}
\ No newline at end of file
# Lambda 表达式
下面这个 lambda 表达式,它的类型不可能是什么?
```java
x -> {
return x %2 == 0;
}
```
## 答案
```java
Function<int, bool>
```
## 选项
### 可以是 Function
```java
Function<Integer, Boolean>
```
### 可以是 Predicate
```java
Predicate<Integer>
```
### 可以是自定义的 SAM
```java
public interface Parsec{
Boolean ask(Integer value);
}
```
### 可以是泛型的 SAM
```java
public interface Parsec<T, R>{
R ask(T value);
}
```
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册