《Java8实战》读书笔记03:Lambda 表达式的组合用法
- 3.8 复合 Lambda 表达式的有用方法
- 1. 比较器复合
- 测试对象
- 比较器 Comparator.comparin
- 比较器链 thenComparing
- 使用 `thenComparing` 拼接`排序条件`
- 使用 `thenComparing` 拼接`比较器`
- 2. 谓词复合 Predicate<T>
- 3. 函数复合 Function<T/,V>
- 参考资料
3.8 复合 Lambda 表达式的有用方法
Comparator
、Function
和 Predicate
都提供了允许你进行复合的方法。可以实现组合逻辑。
链式调用
1. 比较器复合
测试对象
@Data
@AllArgsConstructor
public class Hero{private String name;private Integer age;private String group;public static List<Hero> getList(){return Arrays.asList(new Hero("张三", 23, "A"), new Hero("李四", 19, "A"), new Hero("王五", 20, "B"),new Hero("赵六", 21, "B"),new Hero("洪七", 25, "C"), new Hero("重八", 45, "C"));}
}
比较器 Comparator.comparin
按照 age 升序
public static void main(String[] args) {List<Hero> heroes &#61; Hero.getList();Comparator<Hero> comparing &#61; Comparator.comparing(Hero::getAge);heroes.sort(comparing);heroes.forEach(System.out::println);}
按照 age 降序
创建 comparing
时&#xff0c;多加了一步reversed()
public static void main(String[] args) {List<Hero> heroes &#61; Hero.getList();Comparator<Hero> comparing &#61; Comparator.comparing(Hero::getAge).reversed();heroes.sort(comparing);heroes.forEach(System.out::println);}
比较器链 thenComparing
使用 thenComparing
拼接排序条件
- 先按照
Group
升序。 - 再按照
Age
升序。
public static void main(String[] args) {Hero.getList().sort(Comparator.comparing(Hero::getGroup).thenComparing(Hero::getAge));heroes.forEach(System.out::println);}
使用 thenComparing
拼接比较器
- 先按照
Group
升序。 - 再按照
Age
降序。
import static java.util.Comparator.comparing;
public class Segment01 {public static void main(String[] args) {Hero.getList().sort(comparing(Hero::getGroup).thenComparing(comparing(Hero::getAge).reversed()));heroes.forEach(System.out::println);}
}
2. 谓词复合 Predicate&#xff1c;T&#xff1e;
先来个基础的&#xff1a;age <20 岁
public static void main(String[] args) {Predicate<Hero> hp &#61; hero -> hero.getAge() < 20;Hero.getList().stream().filter(hp).sorted(Comparator.comparing(Hero::getAge)).forEach(System.out::println);}
逻辑非 negate
取反&#xff1a;!(age <20)
public static void main(String[] args) {Predicate<Hero> hp &#61; hero -> hero.getAge() < 20;hp &#61; hp.negate(); Hero.getList().stream().filter(hp).sorted(Comparator.comparing(Hero::getAge)).forEach(System.out::println);}
逻辑与 and
取反 &#43; and&#xff1a;!(age <20) && group &#61;&#61; "B"
public static void main(String[] args) {Predicate<Hero> hp &#61; hero -> hero.getAge() < 20;hp &#61; hp.negate(); hp &#61; hp.and(hero -> "B".equals(hero.getGroup())); Hero.getList().stream().filter(hp).sorted(Comparator.comparing(Hero::getAge)).forEach(System.out::println);}
逻辑或 or
取反 &#43; and &#43; or&#xff1a;!(age <20) && group &#61;&#61; "B" || name &#61;&#61; "重八"
public static void main(String[] args) {Predicate<Hero> hp &#61; hero -> hero.getAge() < 20;hp &#61; hp.negate(); hp &#61; hp.and(hero -> "B".equals(hero.getGroup())); hp &#61; hp.or(hero -> "重八".equals(hero.getName())); Hero.getList().stream().filter(hp).sorted(Comparator.comparing(Hero::getAge)).forEach(System.out::println);}
3. 函数复合 Function&#xff1c;T/,V&#xff1e;
组合两个函数。
修饰符和类型 | 说明 |
---|
default Function | andThen(Function super R,? extends V> after) 返回一个复合函数&#xff0c;该函数首先将此函数应用于其输入&#xff0c;然后将after函数应用于结果。 |
default Function | compose(Function super V,? extends T> before) 返回一个复合函数&#xff0c;该函数首先将before函数应用于其输入&#xff0c;然后将此函数应用于结果。 |
先己后人 andThen
调用 andThen
先自己 f
&#xff0c;再目标 g
Function<Integer, Integer> f &#61; x -> x &#43; 1;Function<Integer, Integer> g &#61; x -> x * 2;int result &#61; f.andThen(g).apply(1);System.out.println(result);
先人后己 compose
调用 compose
先目标 g
&#xff0c;再自己 f
Function<Integer, Integer> f &#61; x -> x &#43; 1;Function<Integer, Integer> g &#61; x -> x * 2;int result &#61; f.compose(g).apply(1);System.out.println(result);
参考资料
比较器 Interface Comparator
断言 Interface Predicate
函数复合 Interface Function