流式编程

函数式接口

Java内置核心四个函数式接口

Consumer< T >
 1package com.atguigu.juc.MyFunction;
2
3import java.util.function.Consumer;
4
5public class FunctionDemo {
6    public static void main(String[] args) {
7        // 匿名内部类
8        Consumer<String> consumer = new Consumer<String>() {
9            @Override
10            public void accept(String s) {
11                System.out.println("匿名内部类方式:我被消费了");
12            }
13        };
14        consumer.accept("张三");
15
16
17        // lambda
18        Consumer<String> consumer1 = (String s) -> {
19            System.out.println("lambda方式:我被消费了");
20        };
21        consumer1.accept("李四");
22
23    }
24}/**Output
25匿名内部类方式:我被消费了
26lambda方式:我被消费了
27*/
//~
Supplier< T >
 1package com.atguigu.juc.MyFunction;
2
3import java.util.function.Consumer;
4import java.util.function.Supplier;
5
6public class FunctionDemo {
7    public static void main(String[] args) {
8        // 匿名内部类
9        Supplier<String> supplier = new Supplier<String>() {
10            @Override
11            public String get() {
12                return "匿名内部类方式";
13            }
14        };
15        System.out.println(supplier.get());
16
17
18        // lambda
19        Supplier<String> supplier1 = () -> {
20            return "lambda表达式方式";
21        };
22        System.out.println(supplier1.get());
23
24    }
25}/**Output
26匿名内部类方式
27lambda表达式方式
28*/
//~
Function< R, T >
 1package com.atguigu.juc.MyFunction;
2
3import java.util.function.Consumer;
4import java.util.function.Function;
5import java.util.function.Supplier;
6
7public class FunctionDemo {
8    public static void main(String[] args) {
9        // 匿名内部类
10        Function<Integer, String> function = new Function<Integer, String>() {
11            @Override
12            public String apply(Integer integer) {
13                return "匿名内部类方式";
14            }
15        };
16        System.out.println(function.apply(2));
17
18
19        // lambda
20        Function<Integer, String> function1 = (Integer integer) -> {
21            return "lambda方式";
22        };
23        System.out.println(function1.apply(2));
24
25    }
26}/**Output
27匿名内部类方式
28lambda方式
29*/
//~

Predicate< T >

 1package com.atguigu.juc.MyFunction;
2
3import java.util.function.Consumer;
4import java.util.function.Function;
5import java.util.function.Predicate;
6import java.util.function.Supplier;
7
8public class FunctionDemo {
9    public static void main(String[] args) {
10        // 匿名内部类
11        Predicate<String> predicate = new Predicate<String>() {
12            @Override
13            public boolean test(String s) {
14                return s.isEmpty();
15            }
16        };
17        System.out.println("匿名内部类方式:" + predicate.test("张三"));
18
19
20        // lambda
21        Predicate<String> predicate1 = (String str) -> {
22            return str.isEmpty();
23        };
24        System.out.println("lambda表达式方式:" + predicate1.test("张三"));
25
26    }
27}/**Output
28匿名内部类方式:false
29lambda表达式方式:false
30*/
//~

流式计算

是什么

是数据渠道,用于操作数据源(集合,数组等)所生成的元素序列.
集合讲的是数据,流讲的是计算

特点
  • Stream自己不会存储元素
  • Stream不会改变源对象.相反,他们会返回一个持有结果的新Stream.
  • Stream操作是延迟执行的.这意味着他们会等到需要结果的时候才执行.
常见几个流式方法
filter(过滤)

接口方法

Stream filter(Predicate predicate);

 1package com.atguigu.juc.bean;
2
3import java.util.Arrays;
4import java.util.List;
5import java.util.stream.Stream;
6
7public class Person {
8    private Integer id;
9    private String name;
10    private Integer age;
11
12    public Person(Integer id, String name, Integer age) {
13        this.id = id;
14        this.name = name;
15        this.age = age;
16    }
17
18    @Override
19    public String toString() {
20        return "Person{" +
21                "id=" + id +
22                ", name='" + name + '\'' +
23                ", age=" + age +
24                '}';
25    }
26
27    public static void main(String[] args) {
28        Person person1 = new Person(11"a"23);
29        Person person2 = new Person(12"b"24);
30        Person person3 = new Person(13"c"22);
31        Person person4 = new Person(14"d"28);
32        Person person5 = new Person(16"e"26);
33        List<Person> list = Arrays.asList(person1, person2, person3, person4, person5);
34        list.stream().filter((Person person) -> {
35            return person.id % 2 == 0;
36        }).filter((Person person) -> {
37            return person.age > 24;
38        // .collect(Collectors.toList()):把Stream转换为List类型的对象
39        }).collect(Collectors.toList())//.forEach(System.out::println);
40
41    }
42}/**Output
43Person{id=14, name='d', age=28}
44Person{id=16, name='e', age=26}
45*/
//~
map(映射)

< R > Stream< R > map(Function< ? super T, ? extends R > mapper);


 1package com.atguigu.juc.bean;
2
3import java.util.Arrays;
4import java.util.List;
5import java.util.stream.Stream;
6
7public class Person {
8
9    public static void main(String[] args) {
10        List<Integer> integerList = Arrays.asList(123);
11        integerList.stream().map((Integer x) -> {
12            return 2 * x;
13        // sorted: 排序,  limit:控制返回的数据数量
14        }).sorted((o1, o2) -> { return o2 - o1;}).limit(2).forEach(System.out::println);
15    }
16}/**Output
176
184
19*/
//~
补充
方法解释
Stream limit(long maxSize)限制流(Stream)最大数量
Stream sorted()排序(从小到大)
Stream sorted(Comparator comparator)自定义排序
long count()统计流(Stream)中的数据量
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值