2022-08-23-jdk8新特性

Lambda基础语法

()—参数列表
-> 分隔
{} 方法体
(a,b)->{
}
无参方法调用
带参数方法调用
(函数接口的参数列表 不需要写类型 需要定义参数名称)->{方法体}

():函数方法参数列表
->分隔 {}方法体
(a,b)->{
Sout(a,b)
}

Lambda语法:
():参数列表
->分隔
{}:方法体
()->{}

Foreach

ArrayList<String> strings = new ArrayList<>();
        strings.add("mayikt");
        strings.add("xiaowei");
        strings.add("xiaomin");
//        strings.forEach(new Consumer() {
//            @Override
//            public void accept(Object o) {
//                System.out.println("o:" + o);
//            }
//        });
        strings.forEach((o) -> {
            System.out.println(o);
        });

Lambda集合排序

public class UserEntity {
    private String name;
    private Integer age;

    public UserEntity(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public Integer getAge() {
        return age;
    }

    @Override
    public String toString() {
        return "UserEntity{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

        ArrayList<UserEntity> userlists = new ArrayList<>();
        userlists.add(new UserEntity("mayikt", 22));
        userlists.add(new UserEntity("xiaomin", 18));
        userlists.add(new UserEntity("xiaoha", 36));
//        userlists.sort(new Comparator<UserEntity>() {
//            @Override
//            public int compare(UserEntity o1, UserEntity o2) {
//                return o1.getAge() - o2.getAge();
//            }
//        });
        userlists.sort((o1, o2) ->
                o1.getAge() - o2.getAge()
        );
        userlists.forEach((Consumer) o -> System.out.println("o:" + o.toString()));



线程调用

new Thread(()-> System.out.println("我是子线程")).start();

Stream创建方式

ArrayList<UserEntity> userEntities = new ArrayList<>();
userEntities.add(new UserEntity("mayikt", 20));
userEntities.add(new UserEntity("meite", 28));
userEntities.add(new UserEntity("zhangsan", 35));
userEntities.add(new UserEntity("xiaowei", 16));
userEntities.add(new UserEntity("xiaowei", 16));

userEntities.stream();
userEntities.parallelStream();

Stream将list转换为Set

Stream<UserEntity> stream = userEntities.stream();
//将我们的集合转为Set
Set<UserEntity> collectSet = stream.collect(Collectors.toSet());
System.out.println(collectSet);


Stream将list转换为Map

import com.mayikt.entity.UserEntity;

import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @ClassName Test001
 * @Author 蚂蚁课堂余胜军 QQ644064779 www.mayikt.com
 * @Version V1.0
 **/
public class Test001 {
    public static void main(String[] args) {
        ArrayList<UserEntity> userEntities = new ArrayList<UserEntity>();
        userEntities.add(new UserEntity("mayikt", 20));
        userEntities.add(new UserEntity("meite", 28));
        userEntities.add(new UserEntity("zhangsan", 35));
        userEntities.add(new UserEntity("xiaowei", 16));
//        userEntities.add(new UserEntity("xiaowei", 16));
//        strings.add("xiaowei");
//        strings.add("xiaomin");
//        strings.add("xiaowei");
        /**
         * 创建一个串行的stream流
         */
        Stream<UserEntity> stream = userEntities.stream();
        // key 为string类型 value UserEntity  集合中的数据:UserEntity , string类型
        Map<String, UserEntity> collect = stream.collect(Collectors.toMap(new Function<UserEntity, String>() {
            @Override
            public String apply(UserEntity userEntity) {
                return userEntity.getUserName();
            }
        }, new Function<UserEntity, UserEntity>() {
            @Override
            public UserEntity apply(UserEntity userEntity) {
                return userEntity;
            }
        }));
        collect.forEach(new BiConsumer<String, UserEntity>() {
            @Override
            public void accept(String s, UserEntity userEntity) {
                System.out.println("s:" + s + ",:" + userEntity.toString());
            }
        });
    }
}

Stream将Reduce 求和

        Stream<Integer> integerStream = Stream.of(10, 30, 80, 60, 10, 70);
//        Optional<Integer> reduce = integerStream.reduce(new BinaryOperator<Integer>() {
//            @Override
//            public Integer apply(Integer a1, Integer a2) {
//                return a1 + a2;
//            }
//        });
        Optional<Integer> reduce = integerStream.reduce((a1, a2) -> a1 + a2);
        System.out.println(reduce.get());
//        Optional<UserEntity> reduce = stream.reduce(new BinaryOperator<UserEntity>() {
//            @Override
//            public UserEntity apply(UserEntity userEntity, UserEntity userEntity2) {
//                userEntity.setAge(userEntity.getAge() + userEntity2.getAge());
//                return userEntity;
//            }
//        });
        Optional<UserEntity> reduce = stream.reduce((user1, user2) -> {
            user1.setAge(user1.getAge() + user2.getAge());
            return user1;
        });



StreamMax和Min

//        Optional<UserEntity> max = stream.max(new Comparator<UserEntity>() {
//            @Override
//            public int compare(UserEntity o1, UserEntity o2) {
//                return o1.getAge() - o2.getAge();
//            }
//        });
        Optional<UserEntity> max = stream.max((o1, o2) -> o1.getAge() - o2.getAge());
        System.out.println(max.get());
Optional<UserEntity> min = stream.min(((o1, o2) -> o1.getAge() - o2.getAge()));
System.out.println(min.get());


StreamMatch 匹配

//        boolean result = stream.noneMatch(new Predicate<UserEntity>() {
//            @Override
//            public boolean test(UserEntity userEntity) {
//                return userEntity.getAge() >35;
//            }
//        });
        boolean result = stream.noneMatch((user) -> user.getAge() > 35);
        System.out.println(result);


StreamFor循环

//        stream.forEach(new Consumer<UserEntity>() {
//            @Override
//            public void accept(UserEntity userEntity) {
//                System.out.println(userEntity.toString());
//            }
//        });
        stream.forEach((userEntity -> System.out.println(userEntity.toString())));


Stream过滤器

//        stream.filter(new Predicate<UserEntity>() {
//            @Override
//            public boolean test(UserEntity userEntity) {
//                return userEntity.getAge() >= 35;
//            }
//        }).filter(new Predicate<UserEntity>() {
//            @Override
//            public boolean test(UserEntity userEntity) {
//                return userEntity.getUserName().equals("zhangsan");
//            }
//        }).forEach(new Consumer<UserEntity>() {
//            @Override
//            public void accept(UserEntity userEntity) {
//                System.out.println(userEntity.toString());
//            }
//        });
        stream.filter((userEntity -> userEntity.getAge() >= 35)).filter(userEntity -> userEntity.equals("zhangsan"))
                .forEach((userEntity -> System.out.println(userEntity.toString())));


Stream排序 sorted

//        stream.sorted(new Comparator<UserEntity>() {
//            @Override
//            public int compare(UserEntity o1, UserEntity o2) {
//                return o1.getAge() - o2.getAge();
//            }
//        }).forEach(new Consumer<UserEntity>() {
//            @Override
//            public void accept(UserEntity userEntity) {
//                System.out.println(userEntity.toString());
//            }
//        });
        stream.sorted(((o1, o2) -> o1.getAge() - o2.getAge())).forEach(userEntity -> System.out.println(userEntity.toString()));


Stream limit和skip

stream.skip(2).limit(1).forEach(userEntity -> System.out.println(userEntity));


并行流与串行流区别

串行流:单线程的方式操作; 数据量比较少的时候。
并行流:多线程方式操作;数据量比较大的时候,原理:
Fork join 将一个大的任务拆分n多个小的子任务并行执行,
最后在统计结果,有可能会非常消耗cpu的资源,确实可以
提高效率。

注意:数据量比较少的情况下,不要使用并行流。

JDK8Optional

判断参数是否为空

Integer a1 = 1;
Optional<Integer> a = Optional.ofNullable(a1);
System.out.println(a.isPresent());

参数为空可以设定默认值

        Integer a1 = 5;
//        Optional<Integer> a = Optional.ofNullable(a1);
//        System.out.println(a.get());
//        System.out.println(a.isPresent());
        Integer a = Optional.ofNullable(a1).orElse(10);
        System.out.println(a);


参数实现过滤

Integer a1 = 16;
Optional<Integer> a = Optional.ofNullable(a1);
boolean isPresent = a.filter(a2 -> a2 > 17).isPresent();
System.out.println(isPresent);


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值