Java8 lambda表达式详解

书欣 Java教程 2022-08-04 17:30:02 13876 1
下文笔者讲述Java8中lambda的简介说明,如下所示:

实现Runnable线程

可以使用下面语法实现Lambda:
 (params) -> expression
 (params) -> statement
 (params) -> { statements }
方法接受两个方法参数
(int t1, int t2) -> t1 + t2
可简写
(int a, int b) -> a + b
例: 多线程示例
Java8之前
new Thread(new Runnable() {
    @Override
    public void run() {
        System.out.println("Java8之前代码写法!");
    }
}).start();

Java8:
    new Thread(() -> System.out.println("java8")).start();
 

遍历List集合

Java8 之前
List<String> numbers = Arrays.asList("j", "a", "v", "a");
for (String number : numbers) {
    System.out.println(number);
}

Java8:
List<String> numbers = Arrays.asList("j", "a", "v", "a");
numbers.forEach(n -> System.out.println(n));
 
或使用方法引用操作符号“::”
List<String> numbers = Arrays.asList("j", "a", "v", "a");
numbers.forEach(System.out::println);

使用过滤方法

filter过滤数组中大于3的数字
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.stream()
        .filter((n) -> n > 3)
        .forEach(System.out::println);
 

复杂的条件

java.util.function.Predicate提供and(), or() 和 xor()可以进行逻辑操作。

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
Predicate<Integer> greater2 = (n) -> n > 2;
Predicate<Integer> less4 = (n) -> n < 4;
numbers.stream()
        .filter(greater2.and(less4))
        .forEach(System.out::println);

实现 Map 和 Reduce

.map 改变集合元素

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
numbers.stream()
        .map((n) -> n * n)
        .forEach(System.out::println);
 
.reduce 将计算集合中所有值。

List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
Integer count = numbers.stream()
        .map((n) -> n * n)
        .reduce((sum, n) -> sum + n)
        .get();
System.out.println(count);
集合转换
.collect 变为另一个集合。

List<Integer> oldNumbers = Arrays.asList(1, 2, 3, 4, 5);
List<Integer> list = oldNumbers.stream()
        .map((n) -> n * n)
        .collect(Collectors.toList());
list.forEach(System.out::println);
 

整合集合中所有元素为一个字符串

将字符串转换为大写
使用逗号串起来
List<String> strList = Arrays.asList("j", "a", "v","a");
String str = strList.stream()
        .map(String::toUpperCase)
        .collect(Collectors.joining(", "));
System.out.println(str);
 

去除重复的元素

distinct()去除重复元素

List<String> stringList = Arrays.asList("a", "b", "c", "a", "b");
String str = stringList.stream()
        .map(String::toUpperCase)
        .distinct()
        .collect(Collectors.joining(", "));
System.out.println(str);
 

计算List中元素的最大值,最小值,和,平均值

List<Integer> numbers = Arrays.asList(2, 3, 5, 7, 11, 12, 14, 16, 17, 18);
IntSummaryStatistics stats = numbers.stream()
        .mapToInt((x) -> x)
        .summaryStatistics();
System.out.println("集合中最大值 : " + stats.getMax());
System.out.println("集合中最小值 : " + stats.getMin());
System.out.println("集合中元素之和 : " + stats.getSum());
System.out.println("集合中元素平均值 : " + stats.getAverage());

排序

创建三个 User 对象,进行姓名排序。

List<User> userList = Arrays.asList(
        new User("A", 12, Sex.MAN, 2011, 60.5, Arrays.asList("swim","running")),
        new User("C", 23, Sex.MAN, 2012, 54.3, Arrays.asList("music","riding")),
        new User("B", 30, Sex.WOMAN, 2010, 48.2, Arrays.asList("swim","programme"))
);

userList.stream()
        .sorted(Comparator.comparing(User::getName))
        .forEach(System.out::println);
 

取最小值

取最小值,不为空则打印
List<User> userList = Arrays.asList(
        new User("A", 12, Sex.MAN, 2011, 60.5, Arrays.asList("swim","running")),
        new User("C", 23, Sex.MAN, 2012, 54.3, Arrays.asList("music","riding")),
        new User("B", 30, Sex.WOMAN, 2010, 48.2, Arrays.asList("swim","programme"))
);

userList.stream()
        .min(Comparator.comparing(User::getAge))
        .ifPresent(System.out::println);
 

取其中一个字段并排序

取其中一个字段并排序
List<User> userList = Arrays.asList(
        new User("A", 12, Sex.MAN, 2011, 60.5, Arrays.asList("swim","running")),
        new User("C", 23, Sex.MAN, 2012, 54.3, Arrays.asList("music","riding")),
        new User("B", 30, Sex.WOMAN, 2010, 48.2, Arrays.asList("swim","programme"))
);

userList.stream()
        .map(User::getName)
        .sorted()
        .forEach(System.out::println);
 

是否符合条件

allMatch是否全部符合条件,是则返回true,但凡有一个不满足则返回false.

List<User> userList = Arrays.asList(
        new User("A", 12, Sex.MAN, 2011, 60.5, Arrays.asList("swim","running")),
        new User("C", 23, Sex.MAN, 2012, 54.3, Arrays.asList("music","riding")),
        new User("B", 30, Sex.WOMAN, 2010, 48.2, Arrays.asList("swim","programme"))
);

boolean tmp = userList.stream()
        .allMatch(u -> u.getAge() < 35);
System.out.println(tmp);
版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。

本文链接: http://www.Java265.com/JavaCourse/202208/4146.html

最近发表

热门文章

好文推荐

Java265.com

//www.java265.com

站长统计|USA-001

Powered By Java265.com信息维护小组

使用手机扫描二维码

关注我们看更多资讯

Honor accompaniments