image.png
    image.png
    image.png
    image.png

    /*
    java内置的4大核心函数接口

    消费型接口 Consumer void accept(T t)
    供给型接口 Supplier T get()
    函数型接口 Function R apply(T t)
    断定型接口 Predicate boolean test(T t)

    1. package com.atguigu.java2;
    2. import org.junit.Test;
    3. import java.util.ArrayList;
    4. import java.util.Arrays;
    5. import java.util.List;
    6. import java.util.function.Consumer;
    7. import java.util.function.Predicate;
    8. /**
    9. * java内置的4大核心函数接口
    10. *
    11. * 消费型接口 Consumer<T> void accept(T t)
    12. * 供给型接口 Supplier<T> T get()
    13. * 函数型接口 Function<T,R> R apply(T t)
    14. * 断定型接口 Predicate<T> boolean test(T t)
    15. *
    16. *
    17. * @author Dxkstart
    18. * @create 2021-06-08 21:40
    19. */
    20. public class LambdaTest2 {
    21. @Test
    22. public void test1(){
    23. happyTime(500, new Consumer<Double>() {
    24. @Override
    25. public void accept(Double aDouble) {
    26. System.out.println("学习太累了,去天上人间买了瓶矿泉水,价格为:" + aDouble);
    27. }
    28. });
    29. System.out.println("***********************");
    30. happyTime(400,money -> System.out.println("学习太累了,去天上人间喝了口水,价格为:" + money));
    31. }
    32. public void happyTime(double money, Consumer<Double> con){
    33. con.accept(money);
    34. }
    35. @Test
    36. public void test2(){
    37. List<String> list = Arrays.asList("北京", "南京", "天津", "东京", "西京", "普京");
    38. List<String> filterStrs = filterString(list, new Predicate<String>() {
    39. @Override
    40. public boolean test(String s) {
    41. return s.contains("京");
    42. }
    43. });
    44. System.out.println(filterStrs);
    45. List<String> filterStrs1 = filterString(list,s -> s.contains("京"));
    46. System.out.println(filterStrs1);
    47. }
    48. //根据给定的规则,过滤集合中的字符串。此规则由Predicate的方法决定
    49. public List<String> filterString(List<String> list, Predicate<String> pre){
    50. ArrayList<String> filterList = new ArrayList<>();
    51. for (String s:list){
    52. if(pre.test(s)){
    53. filterList.add(s);
    54. }
    55. }
    56. return filterList;
    57. }
    58. }