java8新特性 stream Optional

在这里插入图片描述

lamdba

public interface A {
    void add();// 这里的默认的修饰符public所以不用写
    default	 int sum(int a,int b){//使用default修饰符可以写方法体
        return a+b;
    }
    static void sta(){//使用静态修饰符也可以写方法体
        System.out.println("这是一个接口中的静态方法");
    }
}

使用方法 定义的没有修饰符的方法必须要实现,定义default的方法不是必须实现的,可自行根据情况实现,static的方法不能实现覆盖只能直接调用

public class Test implements A{
    public static void main(String[] args) {

        A.sta();//只能这样才能使用接口中定义的静态方法
    }

    @Override
    public void add() {

    }

    @Override
    public int sum(int a, int b) {
        return A.super.sum(a, b);
    }
}

当我们要使用lamdba接口中只能定义一个没有修饰符的方法

   A a=()->System.out.println(“你好");
 ::的写法
A a= System.out::println;

forEach的使用原理

  List<String> strings = Arrays.asList("a", "b", "c");

//这样我们可以看到他又一个的内部类
        strings.forEach(new Consumer<String>() {
            @Override
            public void accept(String s) {
                System.out.println(s);
            }
        });
    //这样我们就能使用lamdbc表达式    
	strings.forEach(s -> System.out.println(s));

根据集合元素进行排序

List<Student> arrayList = new ArrayList<>();
            arrayList.add(new Student(3,"阿威"));
            arrayList.add(new Student(5,"阿牛"));
            arrayList.add(new Student(1,"甲"));
            //使用内部类方法进行排序
            arrayList.sort(new Comparator<Student>() {
                @Override
                public int compare(Student o1, Student o2) {
                    return o1.getId()- o2.getId();
                }
            });
            arrayList.forEach(System.out::println);

            //使用lambda
            arrayList.sort((o1, o2) ->o1.getId()- o2.getId());
            arrayList.forEach(System.out::println);

Strem

在这里插入图片描述
list集合转换set集合
他会自动帮我们去重,

  List<Student> arrayList = new ArrayList<>();
            arrayList.add(new Student(3,"阿威"));
            arrayList.add(new Student(5,"阿牛"));
            arrayList.add(new Student(1,"甲"));
            arrayList.add(new Student(1,"甲"));

            //串行stream()单线程
            //并行parallelStream() 多线程
            //通过流进行转换
        arrayList.stream().collect(Collectors.toSet()).forEach(System.out::println);
        //直接用set集合进行转换
        new HashSet<>(arrayList).forEach(System.out::println);

list转hashMap
以对象中的某个属性作为键,对象作为值进行存储。

     List<Student> arrayList = new ArrayList<>();
            arrayList.add(new Student(3,"阿威"));
            arrayList.add(new Student(5,"阿牛"));
            arrayList.add(new Student(1,"甲"));
           

            //list集合转为hashmap, list.stream().collect(Collectors.toMap(k,v).forEach((k,v)->System.out.println(s+"--"+student);)
        arrayList.stream().collect(Collectors.toMap(student -> student.getName(),student -> student)).forEach((s, student) -> {
            System.out.println(s+"--"+student);
        });

list集合利用reduce进行计算


        List<Student> arrayList = new ArrayList<>();
            arrayList.add(new Student(3,"阿威"));
            arrayList.add(new Student(5,"阿牛"));
            arrayList.add(new Student(1,"甲"));

        //list集合利用reduce进行计算
        Optional<Student> sum = arrayList.stream().reduce((student, student2) -> new Student(student.getId() + student2.getId(), "sum"));
        System.out.println(sum.get().getName());
        System.out.println(sum.get().getId());

查找最大值 最小值

//查找最大值 最小值
        Optional<Student> max = arrayList.stream().max(((o1, o2) -> o1.getId() - o2.getId()));
        System.out.println(max.get());
        Optional<Student> min = arrayList.stream().min(((o1, o2) -> o1.getId() - o2.getId()));
        System.out.println(min.get());

anyMatch 进行匹配查询

boolean b = arrayList.stream().anyMatch(o1 -> "阿威".equals(o1.getName()));
        System.out.println(b);

filter 使用过滤来进行筛选条件

         arrayList.stream().filter(o1 -> "阿牛".equals(o1.getName()) && o1.getId() > 3).forEach(System.out::println);

limit skip
limit进行分页作为结束
skip作为分页的开始

 List<Student> arrayList = new ArrayList<>();
            arrayList.add(new Student(3,"阿威"));
            arrayList.add(new Student(5,"阿牛"));
            arrayList.add(new Student(1,"甲"));

            arrayList.stream().skip(1).limit(3).forEach(System.out::println);

sorted排序

//升序排列
            arrayList.stream().sorted(((o1, o2) -> o1.getId()-o2.getId())).forEach(System.out::println);
            //降序排列
        arrayList.stream().sorted(((o1, o2) -> o2.getId()-o1.getId())).forEach(System.out::println);

方法引用

在这里插入图片描述

静态方法引用
定义接口

public interface A {
    void add(Integer a);
}

我们要想使用静态内部类引用,首先我们需要现在类中定义的静态方法,要注意这个静态方法的放回值类型和参数列表要和接口中的一致

 public static void main(String[] args) {
        //用lambda 方式引用
        A afu=(a)->Test.staticGet(a);
        afu.add(1);
        //使用静态方法引用
        A af=Test::staticGet;
        af.add(1);
    }
    public static void staticGet(Integer a){
        System.out.println("staticGet,a:"+a);

    }

实例方法

public interface A {
    String add(String a);
}

同样实例方法的格式要与接口中的方式一样

  public static void main(String[] args) {
        Test test = new Test();
        //lamdba形式
        A af=(a)->test.as(a);
        System.out.println(af.add("你好"));
        //实例方法引用形式
        A asf=test::as;
        System.out.println(asf.add("你好 阿牛"));
    }
   public String as(String a){
        return a;
   }

构造方法引用

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Student {
    int id;
    String name;
}

要特别注意返回值的类型

@FunctionalInterface
public interface A {
    Student add();
}

public static void main(String[] args) {
        //lamdba形式
        A a=()-> new Student();
        System.out.println(a.add());
        //构造方法的引用
       A a1=Student::new;
        System.out.println(a1.add());
    }

对象方法引用
要注意这里的传递参数 要是使用类的对象类型

@FunctionalInterface
public interface A {
    String add(Test test);
}
 public static void main(String[] args) {
 		//lamdba表达式
        A a=(ad)->ad.as();
        System.out.println(a.add(new Test()));
		//对象方法引用
       A a1=Test::as;
        System.out.println(a1.add(new Test()));
    }

    public String as(){
        return "你好";
    }

java内置函数

 //java内置的函数接口
        Function<String, Integer> stringIntegerFunction = String::length;
        System.out.println(stringIntegerFunction.apply("hello nihao"));

optianal

      String a="null";
        //ofNullable可以传递空值对象
        Optional<String> optional = Optional.ofNullable(a);
        //但是在调用get方法时会报错
        System.out.println(optional.get());
        //判断值是是否为空 返回true代表不为空
        boolean present = optional.isPresent();
        System.out.println(present);
        //of 不能传递空值对象
        Optional<String> a1 = Optional.of(a);
        System.out.println(optional.get());

orElse filter 的使用

        String a=null;//orElse给字符串赋值默认值 只有当他为null的时候才能赋值成功
        String s = Optional.ofNullable(a).orElse("你好");
        System.out.println(s);
        //filter过滤的使用
        boolean present = Optional.ofNullable(a).filter(s1 -> "你好".equals(a)).isPresent();
        System.out.println(present);

案例

Student aw = new Student(1, "AW");
        String aNull = Optional.ofNullable(aw).map(student -> student.getName()).map(s -> s.toLowerCase()).orElse("null");
        System.out.println(aNull);
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值