JAVA8 函数式编程练习

本文探讨JAVA8中引入的函数式编程概念,通过实例解析如何使用Lambda表达式、Stream API进行数据处理,提升代码的简洁性和效率。同时,文章还将介绍函数式接口及其在并行处理中的应用。
摘要由CSDN通过智能技术生成
import javax.swing.text.html.Option;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
//jdk8 函数式编程练习
/*
    函数式编程,四个核心函数
    Function
    Supplier
    Consumer
    Predicate
     */
class Student {

    private String name;
    private int score;

    public Student() {
    }

    public Student(String name, int score){
        this.name = name;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }
}

public class Main3 {
    public static void main(String[] args) {
       List<Student> stus = new ArrayList<>();
       stus.add(new Student("张晓明",25));
       stus.add(new Student("朱德华",36));
       stus.add(new Student("刘小胡",98));
       stus.add(new Student("泽铭",45));

       List<String> names = stus.stream().
               map(Student::getName).
               collect(Collectors.toList());
        System.out.println(names);

        List scores = stus.stream().
                map(Student::getScore).
                filter(x -> x >= 40).
                collect(Collectors.toList());
        stus.stream().filter(s -> s.getScore() > 30).forEach(e -> System.out.println(e.getName()));

        System.out.println(scores);

        Arrays.asList("aaa","vvvv","vd","dddeee")
                .stream()
                .filter(s -> s.length() > 2)
                .forEach(System.out::println);
        Arrays.asList("aaa","vvvv","vd","dddeee")
                .stream()
                .map(s -> s.toUpperCase())
                .forEach(System.out::println);

        System.out.println("------------------------");
        //自然排序,也可传入Comparator进行自定义排序
        Optional op = Arrays.asList("aaa","vvvv","vd","dddeee")
                .stream()
                .sorted((y,x) -> x.compareTo(y))
                .findFirst();//返回一个Optional对象
        //findFirst方法是中止操作,以后不可进行forEach遍历;
        System.out.println(op.get());

        Long cnt = Arrays.asList("aaa","vvvv","vd","dddeee").stream().count();
        System.out.println(cnt);

        //其他两个中止操作, reduce 和Python中的功能相同, collect

        String res = Arrays.asList("aaa","vvvv","vd","dddeee")
                .stream()
                .sorted()
                .reduce("", String::concat);
        System.out.println(res);

        //有没有串的长度大于3的  anyMatch
        boolean flag = Arrays.asList("aaa","vvvv","vd","dddeee")
                .stream()
                .anyMatch(s -> s.length() > 3);

        System.out.println(flag);

        //找到最大值
        Optional<Integer> opt4 = Arrays.asList(1,2,5,6,9).stream()
                .max(Integer::compareTo);//方法引用
        System.out.println(opt4.get());
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值