• 常用的函数式接口


    1. Supplier接口

    生产性接口,指定接口中的泛型为是什么类型,那么接口中的get方法就会产生什么类型的数据。

    例1:输出字符串

     1 package cn.itcast.day_13;
     2 
     3 import java.util.function.Supplier;
     4 
     5 public class DemoSupplier {
     6     public static String getString(Supplier<String> sup){
     7         return sup.get();
     8     }
     9 
    10     public static void main(String[] args) {
    11         String s = getString(() -> {
    12             //生产一个字符串并返回
    13             return "aaaa";
    14         });
    15         System.out.println(s);
    16     }
    17 }

    例2:求数组中最大的数

     1 package cn.itcast.day_13;
     2 
     3 import java.util.function.Supplier;
     4 
     5 public class DemoSupplier02 {
     6     public static int getMax(Supplier<Integer> sup){
     7         return sup.get();
     8     }
     9 
    10     public static void main(String[] args) {
    11         int[] arr = {3, 1, 4, 7, 2, 8};
    12         //调用getMax方法
    13         int maxvalue = getMax(() -> {
    14             int max = arr[0];
    15             for (int i = 0; i < arr.length; i++) {
    16                 if (max < arr[i]){
    17                     max = arr[i];
    18                 }
    19             }
    20             return max;
    21         });
    22 
    23         System.out.println("最大值是" + maxvalue);
    24     }
    25 }

    2. Consumer接口

    例1:对字符串进行反转

     1 package cn.itcast.day_13;
     2 
     3 import java.util.function.Consumer;
     4 
     5 public class DemoConsumer {
     6 
     7     public static void method(String name, Consumer<String> con){
     8         con.accept(name);
     9     }
    10 
    11     public static void main(String[] args) {
    12         method("abc",(String name)->{
    13             //对传递的字符串进行消费
    14             //1.直接输出
    15             System.out.println(name);
    16 
    17             //2.字符串反转
    18             String rename = new StringBuffer(name).reverse().toString();
    19             System.out.println(rename);
    20         });
    21     }
    22 }

    Consum接口中的andThen方法:

    (1)传入两个Consumer接口实现大小写转换:

     1 package cn.itcast.day_13;
     2 
     3 import java.util.function.Consumer;
     4 
     5 public class DemoandThen {
     6     public static void method(String s, Consumer<String> con1, Consumer<String> con2){
     7         con1.accept(s);
     8         con2.accept(s);
     9     }
    10 
    11     public static void main(String[] args) {
    12         method("hello", (String s)->{
    13             System.out.println(s.toUpperCase());
    14         }, (String s)->{
    15             System.out.println(s.toLowerCase());
    16         });
    17     }
    18 }

    (2)使用andThen方法实现大小写转换

     1 package cn.itcast.day_13;
     2 
     3 import java.util.function.Consumer;
     4 
     5 public class DemoandThen {
     6     public static void method(String s, Consumer<String> con1, Consumer<String> con2){
     7         //先对con1进行消费,再对con2进行消费
     8         con1.andThen(con2).accept(s);
     9     }
    10 
    11     public static void main(String[] args) {
    12         method("hello", (String s)->{
    13             System.out.println(s.toUpperCase());
    14         }, (String s)->{
    15             System.out.println(s.toLowerCase());
    16         });
    17     }
    18 }

     使用andThen方法实现格式化输入

     1 package cn.itcast.day_13;
     2 
     3 import java.util.function.Consumer;
     4 
     5 public class DemoConsumer02 {
     6     public static void method(String[] arr, Consumer<String> con1, Consumer<String> con2){
     7         for (String message : arr){
     8             //使用andThen方法连接
     9             con1.andThen(con2).accept(message);
    10         }
    11     }
    12 
    13     public static void main(String[] args) {
    14         String[] arr = {"张三,男", "李四,女", "王五,男"};
    15         method(arr, (message)->{
    16             //消费方式
    17             String name = message.split(",")[0];
    18             System.out.print("姓名:"+name);
    19             System.out.print("---");
    20         }, (message)->{
    21             String xingbie = message.split(",")[1];
    22             System.out.print("性别:"+xingbie+"
    ");
    23         });
    24     }
    25 }

    3. Predicate接口

    用于对某种数据进行判断,返回一个boolean类型

    例:判断一个字符串长度是否大于5

     1 package cn.itcast.day_14;
     2 
     3 import java.util.function.Predicate;
     4 
     5 public class PredicateDemo {
     6     public static boolean checkString(String s, Predicate<String> pre){
     7         return pre.test(s);
     8     }
     9 
    10     public static void main(String[] args) {
    11         String s = "abcdef";
    12         boolean b = checkString(s, (String str) -> {
    13             //对参数传递的字符串进行判断,判断长度是否大于5
    14             return str.length() > 5;
    15         });
    16 
    17         System.out.println(b);
    18     }
    19 }

    predicate中的三个默认方法

    (1)and方法:

    例1:判断字符串长度是否大于5包含a

     1 package cn.itcast.day_14;
     2 
     3 import java.util.function.Predicate;
     4 
     5 public class PredicateDemo02 {
     6     public static boolean checkString(String s, Predicate<String> pre1, Predicate<String> pre2){
     7         return pre1.and(pre2).test(s);
     8     }
     9 
    10     public static void main(String[] args) {
    11         String s = "abcdef";
    12         boolean b = checkString(s, (String str) -> {
    13              return str.length() > 5;
    14         }, (String str) -> {
    15             return  str.contains("a");
    16         });
    17         System.out.println(b);
    18     }
    19 }

    (2)or方法:

    例2:判断字符串长度是否大于5包含a

     1 package cn.itcast.day_14;
     2 
     3 import java.util.function.Predicate;
     4 
     5 public class PredicateDemo03 {
     6     public static boolean checkString(String s, Predicate<String> pre1, Predicate<String> pre2){
     7         return pre1.or(pre2).test(s);
     8     }
     9 
    10     public static void main(String[] args) {
    11         String s = "abcdef";
    12         boolean b = checkString(s, (String str) -> {
    13             return str.length() > 5;
    14         }, (String str) -> {
    15             return str.contains("a");
    16         });
    17         System.out.println(b);
    18     }
    19 }

     (3)!:

    例3:判断字符串长度不大于5

     1 package cn.itcast.day_14;
     2 
     3 import java.util.function.Predicate;
     4 
     5 public class PredicateDemo04 {
     6     public static boolean checkString(String s, Predicate<String> pre){
     7         return !pre.test(s);
     8     }
     9 
    10     public static void main(String[] args) {
    11         String s = "abcdef";
    12         boolean b = checkString(s, (String str) -> {
    13             return str.length() > 5;
    14         });
    15         System.out.println(b);
    16     }
    17 }

    例4:过滤数据

     1 package cn.itcast.day_14;
     2 
     3 import java.util.ArrayList;
     4 import java.util.function.Predicate;
     5 import java.util.zip.ZipEntry;
     6 
     7 public class PredicateDemo05 {
     8     public static ArrayList<String> filter(String[] arr, Predicate<String> pre1, Predicate<String> pre2){
     9         //定义一个Arrarlist集合
    10         ArrayList<String> list = new ArrayList<>();
    11         //获取数组中的每一条信息
    12         for (String s : list){
    13             boolean b = pre1.and(pre2).test(s);
    14             if (b){
    15                 list.add(s);
    16             }
    17         }
    18         return list;
    19     }
    20 
    21     public static void main(String[] args) {
    22         String[] arr = {"张三,男","李四,女","王五,男","赵六,女"};
    23         ArrayList<String> list = filter(arr, (String str) -> {
    24             //获取字符串中性别,判断是否为女
    25             return str.split(",")[1].equals("女");
    26         }, (String str) -> {
    27             return str.split(",")[0].length() == 4;
    28         });
    29 
    30         for (String s : list){
    31             System.out.println(s);
    32         }
    33     }
    34 }

    4. Function接口

    例1:实现字符串转换为数字

     1 package cn.itcast.day_14;
     2 
     3 import java.util.function.Function;
     4 
     5 public class FunctionDemo {
     6     public static void change(String s, Function<String,Integer> fun){
     7         Integer in = fun.apply(s);
     8         System.out.println(in);
     9     }
    10 
    11     public static void main(String[] args) {
    12         String s = "1234";
    13         change(s, (String str)->{
    14             return Integer.parseInt(str);
    15         });
    16     }
    17 }

    Function接口中的andThen方法

    例2:将字符串转为数字,加10后再转换为字符串

     1 package cn.itcast.day_14;
     2 
     3 import java.util.function.Function;
     4 
     5 public class FunctionDemo02 {
     7     public static void change(String s, Function<String,Integer> fun1, Function<Integer,String> fun2){
     8         String s1 = fun1.andThen(fun2).apply(s);
     9         System.out.println(s1);
    10     }
    11 
    12     public static void main(String[] args) {
    13         String s = "123";
    14         change(s, (String str)->{
    15             //将字符转换为整数
    16             return Integer.parseInt(str) + 10;
    17         },(Integer in)->{
    18             //数字转换为字符串
    19             return in+"";
    20         });
    21     }
    22 }

    例3:

  • 相关阅读:
    [BZOJ] 1611: [Usaco2008 Feb]Meteor Shower流星雨
    [BZOJ] 1626: [Usaco2007 Dec]Building Roads 修建道路
    [BZOJ] 1616: [Usaco2008 Mar]Cow Travelling游荡的奶牛
    [BZOJ] 1627: [Usaco2007 Dec]穿越泥地
    [BZOJ] 1614: [Usaco2007 Jan]Telephone Lines架设电话线
    [BZOJ] 1617: [Usaco2008 Mar]River Crossing渡河问题
    [BZOJ] 1612: [Usaco2008 Jan]Cow Contest奶牛的比赛
    [BZOJ] 1625: [Usaco2007 Dec]宝石手镯
    [BZOJ] 1606: [Usaco2008 Dec]Hay For Sale 购买干草
    [HDU] 迷宫城堡 HDU
  • 原文地址:https://www.cnblogs.com/nnnnbbbb1/p/12994043.html
Copyright © 2020-2023  润新知