stream流式处理demo

package com.Test;

import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;

class User{
    private int id;
    private String userName;
    private int age;

    public User(int id,String userName,int age){
        this.id = id;
        this.userName = userName;
        this.age = age;
    }
}

interface MyInterface{
    int myInt(int x);
    String myString(String str);
}

/*
* 请找出偶数id
* 年龄大于24
* 用户名转为大写
* 用户名字母倒排序
* 只输出一个用户名字
* */
public class StreamDemo {

    public static void main(String[] args) {

        User user1 = new User(11, "a", 23);
        User user2 = new User(12, "a", 23);
        User user3 = new User(13, "a", 24);
        User user4 = new User(14, "a", 25);
        User user5 = new User(15, "a", 26);

        List<User> list = Arrays.asList(user1, user2, user3, user4, user5);

        //过滤
        list.stream().filter(t -> { return t.getId() % 2 == 0; })
                .filter(u -> { return u.getAge() > 24; })
                .forEach(System.out::println);

        List<Integer> list2 = Arrays.asList(1,2,3);

        //映射
        list2.stream().map(x -> { return x * 2; }).collect(Collectors.toList());

        list.stream().filter(t -> { return t.getId() % 2 == 0; })
                .filter(u -> { return u.getAge() > 24; })
                .map( m ->{return m.getUserName().toUpperCase();} )
                .sorted((o1,o2) -> {return o2.compareTo(o1); })
                .limit(1).count();
    }

    public void functionExample(){
        //函数型接口
        /*Function<String,Integer> function = new Function<String, Integer>() {
            @Override
            public Integer apply(String s) {
                return 1024;
            }
        };*/
        Function<String,Integer> function = s -> {return s.length();};
        System.out.println(function.apply("abc"));
        function.apply("abc");

        //断定型接口
       /* Predicate<String> predicate = new Predicate<String>() {
            @Override
            public boolean test(String s) {
                return false;
            }
        };*/
        Predicate<String> predicate = s -> {return s.isEmpty();};

        //消费型接口
       /* Consumer<String> consumer = new Consumer<String>() {
            @Override
            public void accept(String s) {

            }
        };*/
        Consumer<String> consumer = s -> {
            System.out.println(s);
        };

        //断定型接口
        /*Supplier<String> supplier = new Supplier() {
            @Override
            public Object get() {
                return "abc";
            }
        };*/

        Supplier<String> supplier = () -> {
            return "abc";
        };
    }
}


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值