• lambda示例


    package function;

    import org.junit.jupiter.api.Test;

    import java.util.ArrayList;
    import java.util.Arrays;
    import java.util.Comparator;
    import java.util.List;
    import java.util.function.Consumer;
    import java.util.function.Predicate;

    public class Function001 {

    @Test
    public void test001() {
    Comparator<Integer> comparable = (o1, o2) -> Integer.compare( o1, o2 );
    System.out.println( comparable.compare( 10, 12 ) );
    Comparator<Integer> com = Integer::compare;
    System.out.println( com.compare( 15, 13 ) );

    }

    @Test
    public void test002() {

    Consumer<String> consumer = (a) -> System.out.println( a + " hello world" );
    consumer.accept( "boy" );
    }

    @Test
    public void test003() {
    MethodTest( 200, (money) -> System.out.println( "价格是:" + money ) );
    }

    public static void MethodTest(int money, Consumer<Integer> con) {
    con.accept( money );
    }

    @Test
    public void test004() {
    List<String> list = Arrays.asList( "北冰洋","北极","太平洋","印度洋","大西洋" );
    // List<String> filterList =filterList( list,s->s.contains( "洋" ) );
    List<String> filterList = filterList( list, Function001::test );
    System.out.println( filterList );
    }

    public static List<String> filterList(List<String> list, Predicate<String> predicate) {
    List<String> filterList = new ArrayList<>();
    for (String s : list) {
    if (predicate.test( s )) {
    filterList.add( s );
    }
    }
    return filterList;
    }

    public static boolean test(String s) {
    return s.contains( "洋" );
    }
    }
  • 相关阅读:
    课后作业之找水王
    SCRUM第二阶段第十天
    第九周总结
    冲刺一3
    用户项目
    预会热词统计
    冲刺一2
    冲刺一(一阶)1
    第八周总结
    小组合作
  • 原文地址:https://www.cnblogs.com/kukai/p/16230561.html
Copyright © 2020-2023  润新知