lambda表达式和Stream流

匿名函数 为了简化Java中的匿名内部类

事件监听 写一个类 实现ActionListener 接口(外部类)

内部类

Lambda 表达式是一个匿名函数,我们可以把 lambda 表达式理解为一段

可以传递的代码(将代码段像数据一样传递)。使用它可以写出更简洁, 更灵

活的代码。作为一种更紧凑的代码风格,使 java 语言的表达式能力得到的提升。

Lambda 表达式的本质只是一个"语法糖",由编译器推断并帮你转换包装为

常规的代码,因此你可以使用更少的代码来实现同样的功能。

public class Demo3 {
    public static void main(String[] args) {
        ArrayList<String> arrayList=new ArrayList<>();
        arrayList.add("c");
        arrayList.add("b");
        arrayList.add("a");
        //
        arrayList.sort(new Comparator<String>() {
            @Override
            public int compare(String o1, String o2) {
                return o1.compareTo(o2);
            }
        });
        //
        arrayList.sort((a,b)->{
            return a.compareTo(b);
        });
        System.out.println(arrayList);
    }
}
@FunctionalInterface
public interface Demo2 {
    int add(int o1, int o2);
}
//@FunctionalInterface 单接口抽象方法,只能存在一个抽象方法

Java8Stream
可以将它看作遍历数据集的高级迭代器

处理数据集合(数组,集合类)‘

对数组,集合类 进行各种操作(过滤,排序.......)

stream处理数据过程

数组/集合类--->流-->各种操作(排序,过滤)-->结果(数组/集合类)

数组和集合类更偏向于存储数据(各种结构)

stream 更偏向于数据操作

流操作:

1.获取流,把集合/数组转换为stream对象

2.流的操作分为:

中间操作:流的各种数据处理

终端操作:把流转换为最终结果(数组/集合/单值)

filter();//过滤流中的某些元素
sorted();//自然排序,流中元素需实现Comparable接口
distinct;//去除重复元素
limit(n);//获取n个元素
skip();//跳过n个元素,配合limit(n)可实现分页
forEach();//遍历流中的元素

public class Demo2 {
    public static void main(String[] args) {
        //集合
        ArrayList<Integer> arrayList=new ArrayList<>();
        arrayList.add(1);
        arrayList.add(2);
        arrayList.add(3);
        arrayList.add(4);
        Stream<Integer> stream =arrayList.stream();
        arrayList.stream().skip(2).limit(2).sorted((o1,o2)->{
            return o2-o1;
        }).forEach((e)->{
            System.out.println(e);
        });
        
        //数组
        Integer [] array=new Integer[]{1,2,3,4};
        Stream<Integer> stream1= Arrays.stream(array);//将数组转为流
 
        Arrays.stream(array).skip(2).limit(2).forEach((e)->{
            System.out.println(e);
        });
 
        Arrays.stream(array).filter((e)->{
            return e<5;
        }).sorted((o1,o2)->{
            return o2-o1;
        }).distinct().forEach((e)->{
            System.out.println(e);
        });
    }
}

终端操作:

Min();返回流中的最小值

Max();返回流中的最大值

count();返回流中元素的个数

reduce();所有元素求和

anyMatch();接收一个predicate函数,只要流中有一个元素满足条件则返回true,否则返回false

allMatch();接收一个predicate函数,当流中每个元素都满足条件则返回true,否则返回false

findFirst();返回流中的第一个元素

		Integer [] array=new Integer[]{1,2,3,2,4};
//Min();返回流中的最小值
        Integer result = Arrays.stream(array).distinct().min((o1,o2)->{
            return o1-o2;
        }).get();
        System.out.println(result);
//Max();返回流中的最大值
        Integer integer=Arrays.stream(array).distinct().max((o1,o2)->{
            return o1-o2;
        }).get();
        System.out.println(integer);
//count();返回流中元素的个数
        long integer1=Arrays.stream(array).distinct().count();
        System.out.println(integer1);
//reduce();所有元素求和
        Integer integer2=Arrays.stream(array).distinct().reduce((a,b)->{
            return a+b;
        }).get();
        System.out.println(integer2);
//anyMatch();接收一个predicate函数,只要流中有一个元素满足条件则返回true,否则返回false
        boolean integer3=Arrays.stream(array).anyMatch((e)->{
           return e>2;
        });
        System.out.println(integer3);
//allMatch();接收一个predicate函数,当流中每个元素都满足条件则返回true,否则返回false
        boolean integer4=Arrays.stream(array).allMatch((e)->{
            return e>2;
        });
        System.out.println(integer4);
//findFirst();返回流中的第一个元素
        Integer integer5=Arrays.stream(array).distinct().sorted((o1,o2)->{
            return o2-o1;
        }).findFirst().get();
        System.out.println(integer5);

collect();将元素存储到集合中

map();将对象中的某个属性映射为一个新元素

toArray();将元素存储到数组中

 ArrayList<Student> arrayList=new ArrayList<>();
        Student student1=new Student(100, "张三1", "男");
        Student student2=new Student(101, "张三2", "男");
        Student student3=new Student(102, "张三3", "男");
        Student student4=new Student(103, "张三4", "男");
        Student student5=new Student(104, "张三5", "男");
        arrayList.add(student1);
        arrayList.add(student2);
        arrayList.add(student3);
        arrayList.add(student4);
        arrayList.add(student5);
 
     List<Student> list= arrayList.stream().sorted((stu1, stu2)->{
            return stu1.getNum()-stu2.getNum();
        }).collect(Collectors.toList());
        System.out.println(list);
 
        System.out.println("------------------------");
       Object[] a = arrayList.stream().sorted((stu1, stu2)->{
            return stu1.getNum()-stu2.getNum();
        }).collect(Collectors.toList())
        .toArray();
        System.out.println(Arrays.toString(a));
 
        System.out.println("----------------------------");
 
 List<Integer> b=arrayList.stream().map(Student::getNum).collect(Collectors.toList());
        System.out.println(b);
        
        System.out.println("----------------------------");
 
System.out.println(arrayList.stream().collect(Collectors.toMap(Student::getNum, Student::getName)));

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值