148 lines
3.2 KiB
Markdown
148 lines
3.2 KiB
Markdown
## 使用Lambda表达式避免重复啰嗦代码
|
||
|
||
```java
|
||
// Java8之前比较两个苹果重量
|
||
Collections.sort(inventory, new Comparator<Apple>) {
|
||
public int compare(Apple a1, Apple a2){
|
||
return a1.getWeight().compareTo(a2.getWeight());
|
||
}
|
||
});
|
||
// Java8之后
|
||
invetory.sort(comparing(Apple::getWeight));
|
||
```
|
||
|
||
通过stream流,支持许多处理数据的并行操作
|
||
|
||
### 流处理
|
||
|
||
linux中的流
|
||
```bash
|
||
# cat命令把两个文件连接起来创建一个流
|
||
# tr装换流中的字符串
|
||
# sort对流进行排序
|
||
# tail -3 给出流最后三行
|
||
# 通过|管道符连接在一起
|
||
cat file1 file2 | tr "[A-Z]" "[a-z]" | sort | tail -3
|
||
```
|
||
|
||
### 行为参数化传递代码给方法
|
||
|
||
把方法作为参数传递给另一个方法
|
||
|
||
### 并行与共享的可变数据
|
||
|
||
避免使用synchronized打破“不能有共享的可变数据”
|
||
|
||
## 函数
|
||
|
||
传统意义方法和类为二等公民,通过运行时传递将方法变为一等公民
|
||
|
||
### 方法和Lambda作为一等公民
|
||
|
||
方法引用
|
||
|
||
```java
|
||
// 筛选隐藏文件
|
||
File[] hiddenFiles = new File(".").listFiles(new FileFilter() {
|
||
public boolean accept(File file) {
|
||
return file.isHidden();
|
||
}
|
||
})
|
||
// Java8
|
||
File[] hiddenFiles = new File(".").listFiles(File::isHidden);
|
||
```
|
||
|
||
Lambda函数
|
||
|
||
(int x) -> x+1
|
||
|
||
### 传递代码
|
||
|
||
```java
|
||
// 筛选绿色苹果
|
||
public static List<Apple> fileterGreenApples(List<Apple> inventory) {
|
||
List<Apple> resutl = new ArrayList<>();
|
||
for (Apple apple: inventory) {
|
||
if ("green".equals(apple.getColor())) {
|
||
result.add(apple);
|
||
}
|
||
}
|
||
return result;
|
||
}
|
||
|
||
// 筛选重的苹果
|
||
public static List<Apple> fileterHeavyApples(List<Apple> inventory) {
|
||
List<Apple> resutl = new ArrayList<>();
|
||
for (Apple apple: inventory) {
|
||
if (apple.getWeight() > 150) {
|
||
result.add(apple);
|
||
}
|
||
}
|
||
return result;
|
||
}
|
||
|
||
// Java8
|
||
public static boolean isGreenApple(Apple apple) {
|
||
return "green".equals(apple.getColor());
|
||
}
|
||
public static boolean isHeavyApple(Apple apple) {
|
||
return apple.getWeight() > 150;
|
||
}
|
||
public interface Predicate<T>{
|
||
boolean test(T t);
|
||
}
|
||
static List<Apple> filterApples(List<Apple> inventory,
|
||
predicate<Apple> p) {
|
||
List<Apple> resutl = new ArrayList<>();
|
||
for (Apple apple: inventory) {
|
||
if (p.test(apple)) {
|
||
result.add(apple);
|
||
}
|
||
}
|
||
return result;
|
||
}
|
||
|
||
filterApples(inventory, Apple::isGreenApple);
|
||
filterApples(inventory, Apple::isHeavyApple);
|
||
```
|
||
|
||
### 从传递方法到Lambda
|
||
|
||
匿名函数
|
||
|
||
```java
|
||
filterApples(inventory, (Apple a) -> "green".equals(a.getColor()));
|
||
filterApples(inventory, (Apple a) -> a.getColor() > 150);
|
||
filterApples(inventory, (Apple a) -> a.getColor() < 80 ||
|
||
"brown".equals(a.getColor()));
|
||
```
|
||
|
||
## 流
|
||
|
||
```java
|
||
// 顺序处理:
|
||
|
||
import static java.util.stream.Collectors.toList;
|
||
List<Apple> heavyApples=
|
||
inventory.stream().filter((Apple a)-> a.getWeight() > 150)
|
||
.collect(toList());
|
||
|
||
//并行处理:
|
||
|
||
import static java.util.stream.Collectors.toList;
|
||
List<Apple> heavyApples=
|
||
inventory.parallelStream().filter((Apple a)-> a.getWeight() > 150)
|
||
.collect(toList());
|
||
|
||
```
|
||
|
||
## 默认方法
|
||
|
||
支持库设计师,写出更容易改进的接口。
|
||
给接口添加一个新方法,意味着所有的实体类都必须为其提供一个实现
|
||
|
||
## 函数式编程的其他好思想
|
||
|
||
通过使用更多描述性数据类型来避免null
|
||
|
||
结构(模式)匹配 |