Java基础16(函数式接口,Stream流)

函数式接口

函数式接口:指有且仅有一个抽象方法的接口。
Java中的函数式编程体现就是Lambda表达式,所以函数式接口就是可以适用于Lambda使用的接口,只有确保接口中有且仅有一个抽象方法,Java中的Lambda才能顺利地进行推导。
函数式接口注解:

  • @FunctionalInterface
  • 放在函数式接口的上方,如果接口是函数式接口则编译通过,反之编译失败。

函数式接口作为方法的参数

如果方法的参数是一个函数表达式,可以使用Lambda表达式作为该方法的参数传递

函数式接口作为方法的返回值

如果方法的返回值是一个函数式接口,我们可以使用Lambda表达式作为返回结果

public class ComparatorDemo {
    public static void main(String[] args) {
        //创建数组
        ArrayList<String> arrayList = new ArrayList<String>();
        arrayList.add("Marry");
        arrayList.add("Bob");
        arrayList.add("Alen");
        System.out.println("排序前"+ arrayList);
        Collections.sort(arrayList,getComparator());
        System.out.println("排序后"+ arrayList);
    }
    private static Comparator<String> getComparator(){
        //写法一,重写compare(String s1, String s2)方法
//        Comparator<String> comparator = new Comparator<String>() {
//            @Override
//            public int compare(String s1, String s2) {
//                return s1.length()-s2.length();
//            }
//        };
//        return comparator;
        //写法二,匿名内部类
//        return new Comparator<String>() {
//            @Override
//            public int compare(String s1, String s2) {
//                return s1.length()-s2.length();
//            }
//        };
        //写法三,Lambda表达式
        return ( s1, s2) -> s2.length()-s1.length();

    }
}

常用函数式接口之Supplier

Supplier:包含一个无参的方法

  • T get():获得结果
  • 该方法不需要参数,它会按照某种实现逻辑(由Lambda表达式实现)返回一个数据
  • Supplier接口也被称为生产型接口,如果我们指定了接口的泛型是什么类型,那么接口中的get方法就会生产什么类型的数据供我们使用
//返回一个集合数组中的最大值
public class SupplierDemo {
    public static void main(String[] args) {
        ArrayList<Integer> arrayList = new ArrayList<Integer>();
        arrayList.add(13);
        arrayList.add(23);
        arrayList.add(42);
        arrayList.add(91);
        arrayList.add(64);
        Collections.sort(arrayList);

        int result = getMaxValue(() -> arrayList.get(arrayList.size() - 1));
        System.out.println(result);
    }
    private static Integer getMaxValue(Supplier<Integer> supplier){
        return supplier.get();
    }
}

常用函数式接口之Consumer

Consumer:包含两个方法

  • void accept(T t):对给定的参数执行此操作
  • default Consumer<T> andThen(Consumer after):返回一个组合的Consumer,依次执行此操作,然后执行after操作
  • Consumer<T> 接口也被称为消费型接口,它消费的数据的数据类型由泛型指定
public class ConsumerDemo {
    public static void main(String[] args) {
        String[] arr = {"张三,24","王五,22","赵六,21","李四,28"};
        for (int i = 0; i< arr.length; i++){
            String info = arr[i];
            printString(info,
                    (String str) -> {
                        System.out.print("姓名:"+str.split(",")[0]+",");
                    },
                    (String str) -> {
                        System.out.println("年龄:"+str.split(",")[1]);
                    });

        }


    }
    private static void printString(String info, Consumer<String> consumer,Consumer<String> consumer1){
        consumer.andThen(consumer1).accept(info);
    }
}

常用函数式接口之Predicate

Predicate:常用的四个方法

  • boolean test(T t):给指定的参数进行判断(判断逻辑由Lambda表达式实现),返回一个布尔值
  • default Predicate<T> negate():返回一个逻辑的否定,对应逻辑非
  • default Predicate<T> and(Predicate other):返回一个组合判断,对应短路与
  • default Predicate<T> or(Predicate other):返回一个组合判断,对应短路或
  • Predicate<T>:接口通常用于判断参数是否满足指定的条件
/*筛选数组中名字长度大于2的成年人到ArrayList数组中,并遍历数组*/
public class PredicateDemo {
    public static void main(String[] args) {
        String[] strArray = {"张给三,26","李四,21","王额五,20","赵六,19","孙的七,24"};
        ArrayList<String> arrayList = new ArrayList<String>();
        for (String s : strArray
             ) {
            //筛选数组中名字长度大于2的成年人到ArrayList数组中
            boolean result = checkStr( s,
                    s1 -> {int len = s.split(",")[0].length();
                        return  len > 2;},
                    s1 -> {int age = Integer.parseInt(s.split(",")[1]);
                    return age > 21;});
            if (result){
                arrayList.add(s);
            }

        }
        //遍历数组集合
        for (String stringArrayList : arrayList
             ) {
            System.out.println(stringArrayList);
        }
    }
    private static boolean checkStr(String string, Predicate<String> predicate,Predicate<String> predicate1){
        return predicate.and(predicate1).test(string);
    }
}

常用函数式接口之Function

Function<T,R>:常用的两个方法

  • R apply(T t):将此函数应用于给定的参数
  • default <V> Function andThen(Function after):返回一个组合函数,首先将该函数应用于输入,然后将after函数应用于结果
  • Function<T,R> 接口通常用于对参数进行处理,转换(逻辑由Lambda表达式实现),然后返回一个新的值
public class FunctionDemo {
    public static void main(String[] args) {
        String s = "张三,22";
        int result = convert(s,
                s1 -> Integer.parseInt(s.split(",")[1]),
                i ->  i + 70);
        System.out.println(result);

    }
    public static int convert(String s, Function<String,Integer> function,Function<Integer,Integer> function1){
        return function.andThen(function1)
                .apply(s);
    }
}

Stream流的使用

  • 生成流,通过数据源,(集合,数组等)生成流,例如list.stream()
  • 中间操作,一个流后面可以跟随零个或者多个中间操作,其目的主要是打开流,做出某种程度的数据过滤/映射,然后返回一个新的流,交给下一个操作使用,例如filter()
  • 终结操作,一个流智能有一个终结操作,当这个操作执行后,流就被使用“光”了,无法再被操作。所以这必定是流的最后一个操作,例如forEach()
Stream流的常见生成方式
  • Conllection体系的集合可以使用默认方法stream()生成流,default Stream <E> stream()
  • Map体系的集合间接地生成流
  • 数组可以通过Stream接口的静态方法of(T...values)生成流
public class CreateStream {
    public static void main(String[] args) {
        //Conllection体系的集合可以使用默认方法stream()生成流,default Stream <E> stream()
        List<String> list = new ArrayList<String>();
        Stream<String> listStream = list.stream();
        HashSet<String> setStream = new HashSet<>();
        //Map体系的集合间接地生成流
        HashMap<String, Integer> hashMap = new HashMap<>();
        Stream<String> keyStream = hashMap.keySet().stream();
        Stream<Integer> valuewStream = hashMap.values().stream();
        Stream<Map.Entry<String, Integer>> entryStream = hashMap.entrySet().stream();
        //数组可以通过Stream接口的静态方法of(T...values)生成流
        String[] strArray = {"张三","李四","王五"};
        Stream<String> arrStream = Stream.of(strArray);
        //可变参数直接生成数组流
        Stream<Object> objectStream = Stream.of("Tom", "Bob", "Marry");
        Stream<Object> objectStream1 = Stream.of(10, 100, 1000);
    }
}
Stream流中间操作之filter
Stream<T> filter(Predicate<? super T> predicate);:用于对流中的数据进行过滤
public static void main(String[] args) {
        ArrayList<String> arrayList = new ArrayList<String>();
        arrayList.add("张三");
        arrayList.add("李四");
        arrayList.add("王五");
        arrayList.add("李涛涛");
        arrayList.add("李简洁");
        //Stream流过滤以李开头,三个字的名字,并遍历
        arrayList.stream().filter(s -> s.startsWith("李")).filter(s -> s.length() == 3).forEach(System.out::println);
    }
Stream流中间操作之limit&skip
  • Stream<T> limit(long maxSize):返回此流中的元素组成的流,截取前指定参数个数的数据
  • Stream<T> skip(long n);:跳过指定参数个数的数据,返回由该流的剩余元素组成的流
public static void main(String[] args) {
        ArrayList<String> arrayList = new ArrayList<String>();
        arrayList.add("张三");
        arrayList.add("李四");
        arrayList.add("王五");
        arrayList.add("李涛涛");
        arrayList.add("李简洁");
        System.out.println(arrayList);
//        arrayList.stream().limit(3).forEach(System.out::println);
        arrayList.stream().skip(1).forEach(System.out::println);//从arrayList.get(0)开始,跳过几个
    }
Stream流中间操作之concat&distinct
  • static <T> Stream<T> concat(Stream<? extends T> a, Stream<? extends T> b),合并两个流为一个流,并返回
  • Stream<T> distinct();返回由该流的不同(不重复)元素(根据Object.equals(Object))组成的流
public class ConcatAndDistinctDemo {
    public static void main(String[] args) {
        ArrayList<String> arrayList = new ArrayList<String>();
        arrayList.add("张三");
        arrayList.add("李四");
        arrayList.add("王五");
        arrayList.add("李涛涛");
        arrayList.add("李简洁");
        //取前3位元素,组成一个流
        Stream<String> limitStream = arrayList.stream().limit(3);
        //跳过前2位元素,组成一个流
        Stream<String> skipStream = arrayList.stream().skip(2);
        //合并上面得到的两个流,得到一个新的流,输出在控制台
//       Stream.concat(limitStream, skipStream).forEach(System.out::println);
        //将合并后的流的重复元素去除后再输出在控制台
        Stream.concat(limitStream, skipStream).distinct().forEach(System.out::println);
    }
}
Stream流中间操作之sorted
  • Stream<T> sorted();:返回由该流的元素组成的流,根据自然顺序排序
  • Stream<T> sorted(Comparator<? super T> comparator);,返回由该流的元素组成的流,根据提供的Comparator排序
public class StreamSortedDemo {
    public static void main(String[] args) {
        ArrayList<String> arrayList = new ArrayList<String>();
        arrayList.add("Tom");
        arrayList.add("Bob");
        arrayList.add("Alen");
        arrayList.add("Jinx");
        arrayList.add("Lux");
        //根据名字的自然顺序排序
//        arrayList.stream().sorted().forEach(System.out::println);
        //根据名字的长短排序,长短相同时根据自然顺序排序
        arrayList.stream().sorted((s1,s2) -> {
            int num = s1.length() - s2.length();
            int num1 = num == 0? s1.compareTo(s2):num;
            return num1;
        }).forEach(System.out::println);
    }
}
Stream流中间操作之map&mapToInt
  • <R> Stream<R> map(Function<? super T, ? extends R> mapper);:返回由给定函数应用于此流的元素的结果组成的流
  • IntStream mapToInt(ToIntFunction<? super T> mapper);:返回一个IntStream其中包含将指定函数应用于此流的元素的结果
  • IntSream表示原始int流,ToIntFunction的中的方法:int applyAsInt(T value);
  • IntStream中特有的sum()方法,返回流中元素的总和
public class StreamMapAndMapToIntDemo {
    public static void main(String[] args) {
        ArrayList<String> arrayList = new ArrayList<String>();
        arrayList.add("14");
        arrayList.add("45");
        arrayList.add("64");
        arrayList.add("356");
        arrayList.add("243");
        //将集合中的字符串数组转换成整数在控制台输出
//        arrayList.stream().map((String s) -> {
//            int result = Integer.parseInt(s);
//            return result;}).forEach(System.out::println);
        //优化
//        arrayList.stream().map(Integer::parseInt).forEach(System.out::println);
        //求和
        int result = arrayList.stream().mapToInt(Integer::parseInt).sum();
        System.out.println(result);
    }
}
Stream流终结操作操作之foreach&count
  • void forEach(Consumer<? super T> action):对此流中的每一个元素执行操作
  • long count();返回此流中元素的个数
public class ExerciseDemo {
    public static void main(String[] args) {
        //两个ArrayList分别存储6位男演员与6位女演员的名称
        ArrayList<String> actorNameArrayList = new ArrayList<>();
        actorNameArrayList.add("成龙");
        actorNameArrayList.add("李连杰");
        actorNameArrayList.add("吴京");
        actorNameArrayList.add("洪金宝");
        actorNameArrayList.add("吴彦祖");
        actorNameArrayList.add("王千源");
        ArrayList<String> actressNameArrayList = new ArrayList<>();
        actressNameArrayList.add("林青霞");
        actressNameArrayList.add("林心如");
        actressNameArrayList.add("刘诗诗");
        actressNameArrayList.add("林妙可");
        actressNameArrayList.add("张柏芝");
        actressNameArrayList.add("林允");
        //男演员只要名字为三个字的前三人
        Stream<String> actorRosterStream = actorNameArrayList.stream().filter(s -> s.length() == 3).limit(3);
        //女演员只要姓林的,并且不要第一个
        Stream<String>  actressRosterStream= actressNameArrayList.stream().filter(s -> s.startsWith("林")).skip(1);
        //把过滤后的男女演员名字混合在一起,将混合流中的元素作为构造方法的参数创建对象
        Stream.concat(actorRosterStream, actressRosterStream)
                .map(Actor::new)
                .forEach(a -> {
            String name = a.getName();
            System.out.println(name);
        });
    }
}
Stream流的收集操作

对数据使用Stream流的操作之后,如何把流中的数据收集到集合中

  • <R, A> R collect(Collector<? super T, A, R> collector)

工具类Collectors提供了具体的收集方式

  • public static <T> Collector<T, ?, List<T>> toList()
  • public static <T> Collector<T, ?, List<T>> toSet()
  • public static <T> Collector<T, ?, Map<K,U>> toMap(Function<? super T, ? extends K> keyMapper, Function<? super T, ? extends U> valueMapper)
public class StreamCollectDemo {
    public static void main(String[] args) {
        ArrayList<String> actorNameArrayList = new ArrayList<>();
        actorNameArrayList.add("成龙");
        actorNameArrayList.add("李连杰");
        actorNameArrayList.add("吴京");
        actorNameArrayList.add("洪金宝");
        actorNameArrayList.add("吴彦祖");
        actorNameArrayList.add("王千源");
        Stream<String> nameStream = actorNameArrayList.stream().filter(s -> s.startsWith("吴"));
        List<String> streamToList = nameStream.collect(Collectors.toList());
        for (String s: streamToList){
            System.out.println(s);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值