2021-07-27

方法引用

函数式接口
接口中只与一个必须被重写的抽象方法
@FunctionalInterface

四大内置函数式接口:
1.消费型接口 Consumer
void accept(T t) --> 有来无回,有参数没有返回值
2.供给型接口 Supplier
T get()
3.函数型接口 Function<T,R>
R apply(T t)
4.段言型接口 Predicate
boolean test(T t)

方法引用
当lambda体的实现,是通过调用其他方法实现的,可以通过方法引用的方式实现简化Lambda表达式

是lambda表达式的另外一种表现形式,简化Lambda表达式 ()->{}

语法: (:😃
对象::成员方法名
1)lambda体的实现是否是通过调用了另外一个方法实现的 --> √
2)内部所引用方法的返回值是否与lambda的返回值保持一致 --> √
类名::静态方法名

​ 1)lambda体的实现是否是通过调用了另外一个方法实现的 --> √

​ 2)内部所引用方法的返回值是否与lambda的返回值保持一致 --> √

​ 类名::成员方法名

​ 1)lambda体的实现是否是通过调用了另外一个方法实现的 --> √

​ 2) 内部所引用方法的返回值与lambda的返回值相同
​ lambda参数列表的第一个参数作为内部引用成员方法的对象存在
lmabda参数列表的第二个参数开始(如果有的话)作为内部引用方法的参数列表

例:

import java.io.PrintStream;
import java.util.Comparator;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.BiPredicate;

public class Class002_FunctionQuite {
    public static void main(String[] args) {
        test3();
    }
    //类名::成员方法名
    public static void test3(){
        //BiPredicate<String,String> pre = (x,y)-> x.equals(y);
        /*
             分析:  通过类名::成员方法名 简化lanbda结构
                1)lambda体的实现是否是通过调用了另外一个方法实现的  --> √
                2) 内部所引用方法的返回值与lambda的返回值相同
                   lambda参数列表的第一个参数作为内部引用成员方法的对象存在
                   lmabda参数列表的第二个参数开始(如果有的话)作为内部引用方法的参数列表
         */
        BiPredicate<String,String> pre = String::equals;
        System.out.println(pre.test("张三","张三封"));;
    }


    //类名::静态方法名
    public static void test2(){
        //求两个数中最大的值
        //BiFunction<Integer,Integer,Integer> function = (x,y)-> Math.max(x,y);
        /*
             分析:
                1)lambda体的实现是否是通过调用了另外一个方法实现的  --> √
                2)内部所引用方法的返回值是否与lambda的返回值保持一致 --> √
         */
        BiFunction<Integer,Integer,Integer> function = Math::max;

        System.out.println(function.apply(100,101));;

        //Comparator<Integer> com  = (x,y) -> {return Integer.compare(x,y);};
        /*
             分析:
                1)lambda体的实现是否是通过调用了另外一个方法实现的  --> √
                2)内部所引用方法的返回值是否与lambda的返回值保持一致 --> √
         */
        Comparator<Integer> com  = Integer::compareTo;

        System.out.println(com.compare(18,18));;

    }

    //对象::成员方法名
    public static void test1(){
        List<Integer> list = List.of(1,2,3,4,5);

        list.forEach(x->{
            System.out.println(x);
        });

        PrintStream ps = System.out;

        list.forEach(x->{
            ps.println(x);
        });
        /*
            分析:
                1)lambda体的实现是否是通过调用了另外一个方法实现的  --> √
                2)内部所引用方法的返回值是否与lambda的返回值保持一致 --> √
         */
        list.forEach(ps::println);
        list.forEach(System.out::println);
    }
}

构造器引用–> 类名::new

数组引用–> 类型[]::new

import java.util.Arrays;
import java.util.function.Function;
import java.util.function.Supplier;

public class Class003_FunctionQuite {
    public static void main(String[] args) {
        //构造器引用
        //Supplier<Employee> sup = ()-> new Employee();
        //1) lambda体中创建了对象.引用了某一个构造器  √
        //2) 所引用的构造器的参数列表是否与lambda的参数列表一致
           //创建的对象作为lambda返回值
        Supplier<Employee> sup = Employee::new;
        System.out.println(sup.get());

        Function<String,Employee> fun = s -> new Employee(s);
        fun = Employee::new;
        System.out.println(fun.apply("yinwei"));

        //数组引用
        Function<Integer,Integer[]> fun2 = i -> new Integer[i];
         fun2 =Integer[]::new;

        System.out.println(Arrays.toString(fun2.apply(5)));
    }
}

class Employee{
    private String  name;
    private int num;

    public Employee() {
    }

    public Employee(String name) {
        this.name = name;
    }

    public Employee(String name, int num) {
        this.name = name;
        this.num = num;
    }

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

Stream流

Stream流:
根据数据源所产生的的元素序列
数据源: 集合,数组 —> 侧重点在数据的存储
stream流: -->关注数据的计算

注意:
1.流本身不能存储数据
2.流不能修改数据源中的数据
3.流是一次性的流,流是式操作的每一步都会返回一个持有结果的新流
4.延迟执行|惰性加载 : 当不进行终止行为时候,不会执行流式中间操作

过程:
1.获取|创建stream
2.流式中间操作
3.终止行为(返回的结果不在是stream)

1.获取|创建stream

public static void main(String[] args) {
        //1.Collection--> stream
        List<Integer> list = new ArrayList<>();
        //顺序流
        Stream<Integer> stream = list.stream();
        //并行流
        Stream<Integer> stream2 = list.parallelStream();

        System.out.println(stream2);

        //2) Arrays.stream(array)
        Stream stream3 = Arrays.stream(new String[]{"1"});
        System.out.println(stream3);

        //3)Stream.of(数据1,数据2,数据3...)
        Stream stream4 = Stream.of("a","b");
        stream4.forEach(System.out::println);

        //4)Stream.of(数组)
        Stream stream5 =  Stream.of(new String[]{"abc","bcd"});
        stream5.forEach(System.out::println);

    }

2.流式中间操作

​ a.筛选和切片–>filter()、limit()、skip()、distinct()……

​ b.排序–> 1.sorted(Comparable)-自然排序

​ 2.sorted(Comparator)-定制排序

import com.yjx.entity.Employee;
import java.util.List;
import java.util.stream.Stream;

public class Class02_Stream {
    private static List<Employee> list=List.of(
            new Employee(101,"张三",18,1111.555),
            new Employee(103,"李四",19,3111.555),
            new Employee(101,"张三",18,1111.555),
            new Employee(102,"赵王",16,2111.555),
            new Employee(104,"王六",19,5111.555),
            new Employee(106,"老八",20,4111.555),
            new Employee(105,"啤酒",18,6111.555)
    );

    public static void main(String[] args) {
        //获取|创建流
        Stream<Employee> stream1=list.stream();
        //操作流
        Stream<Employee> stream2=stream1
                .filter(e->{
                    //System.out.println("正在筛选");
                    return e.getAge()>=18;})
                .limit(5)
                .skip(1)
                .distinct()
                .sorted()
                //.sorted((x,y)->Double.compare(x.getPrice(),y.getPrice()));
        ;
        //终止行为
        stream2.forEach(System.out::println);
    }
}

map()与flatMap()

    public static void main(String[] args) {
        //获取|创建流

        Stream<Employee> stream1=list.stream();
        //操作流
        //1) 获取员工姓名的流
        Stream stream2=stream1.map((employee)->{return employee.getName();})
            .distinct();
        //终止行为
        stream2.forEach(System.out::print);
        System.out.println();


        List<String> list = List.of("aaa","bbb","ccc");
        Stream<Character> stream3=list.stream()
                .flatMap(Class03_Stream_map::getCharacterStream);
        stream3.forEach(System.out::println);
        //map
        List<Stream> list1=list.stream()
                .map(Class03_Stream_map::getCharacterStream)
                .collect(Collectors.toList());
        for (Stream st :
                list1) {
            st.forEach(System.out::println);
        }
    }

3.终止行为

​ a. 查找与匹配

​ b. 收集(了解)

public void test1() {
// allMatch-检查是否匹配所有元素
boolean b1 = emps.stream()
.allMatch((e) -> e.getStatus().equals(Status.BUSY));
System.out.println(b1);
// anyMatch-检查是否至少匹配一个元素
boolean b2 = emps.stream()
.anyMatch((e) -> e.getStatus().equals(Status.BUSY));
System.out.println(b2);
// noneMatch-检查是否没有匹配所有元素
boolean b3 = emps.stream()
.noneMatch((e) -> e.getStatus().equals(Status.OTHER));
System.out.println(b3);
// findFirst-返回第一个元素
// 需求:按工资排序,获取第一个员工信息
Optional<Employee> op1 = emps.stream()
.sorted((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()))
.findFirst();
System.out.println(op1.get());
// findAny-返回当前流中的任意元素
// 需求:找一个空闲状态的员工,添加到开发团队中
Optional<Employee> op2 = emps.parallelStream()// 并行流-多条线程进行,谁先找到就是.filter((e) -> e.getStatus().equals(Status.FERR))
.findAny();
System.out.println(op2.get());
// count-返回流中元素的总个数
Long count = emps.stream().count();
System.out.println(count);
// max-返回流中最大值
// 需求:获取工资最高的员工信息
Optional<Employee> op3 = emps.stream()
.max((e1, e2) -> Double.compare(e1.getSalary(), e2.getSalary()));
System.out.println(op3.get());
// min-返回流中最小值
// 需求:获取公司中工资最少员工的工资
Optional<Double> op4 = emps.stream()
.map(Employee::getSalary)
.min(Double::compare);
System.out.println(op4.get());
}

收集-将流转换为其他形式,接收一个Collertor接口的实现,用于给Stream中元素做汇总的方法

// 需求:获取当前公司所有员工的姓名添加到集合中
// List-把流中所有元素收集到List中
List<String> list = emps.stream()
.map(Employee::getName)
.collect(Collectors.toList());
list.forEach(System.out::println);

// Set-把流中所有元素收集到Set中,删除重复项
Set<String> set = emps.stream()
.map(Employee::getName)
.collect(Collectors.toSet());
set.forEach(System.out::println);

// Map-把流中所有元素收集到Map中,当出现相同的key时会抛异常
Map<String, Integer> map = emps.stream()
.collect(Collectors.toMap(Employee::getName, Employee::getAge));
System.out.println(map);


// 员工总数
Long count = emps.stream()
.collect(Collectors.counting());
System.out.println(count);
// 工资平均数
Double avg = emps.stream()
.collect(Collectors.averagingDouble(Employee::getSalary));
System.out.println(avg);
// 工资总和
Double sum = emps.stream()
.collect(Collectors.summingDouble(Employee::getSalary));
System.out.println(sum);
// 工资最大值的员工
Optional<Employee> op = emps.stream()
.collect(Collectors.maxBy((e1, e2) -> Double.compare(e1.getSalary(),
e2.getSalary())));
System.out.println(op.get());

注意:
1、Stream自己不会存储元素·
2、Stream不会改变源对象。相反,会返回一个持有结果的新Stream。

​ 3、Stream操作是延迟执行的,这意味着他们会等到需要结果的时候才执行。

Oracle Database

oracle XE安装与卸载,测试

数据库了解

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值