美文网首页
Lambda表达式基础语法

Lambda表达式基础语法

作者: MrL槑槑 | 来源:发表于2018-07-09 21:29 被阅读0次
@FunctionalInterface
public interface MyFuntion<Integer> {

    java.lang.Integer getValue(java.lang.Integer t);

}
/**
 * 一、Lambda 表达式基础语法:JAVA8中引入新的操作符 "->" 该操作符成为箭头操作符或者lambda操作符
 *         主要包括两个方面:
 *
 * 左侧:Lambda表达式的参数列表
 * 右侧:Lambda表达式中所需要执行的功能,即lambda表达体
 *
 * 语法格式一:无参无返回值
 *      () -> System.out.println("hello world");
 *
 * 语法格式二:有参数无返回值
 *        (x) -> System.out.println("hello world");
 *
 * 语法格式三:只有一个参数、可以去掉括号
 *      x -> System.out.println("hello world");
 *
 * 语法格式四:多个参数,lambda体中有多条语句,有返回值
 *      Comparator<Integer> comparator = (o1, o2)->{
 *             System.out.println(o1-o2);
 *             return Integer.compare(o1,o2);
 *         };
 *
 * 语法格式五:若lambda体中只有一条语句,则return、{} 都可省
 *      Comparator<Integer> comparator = (o1, o2)->Integer.compare(o1,o2);
 *
 * 语法格式六:lambda表达式的参数列表的数据类型可以省略不写,因为JVM编译器可以通过上下文推断出数据类型,
 *      即:类型推导
 *          Comparator<Integer> comparator1 = (o1, o2)->Integer.compare(o1,o2);
 *
 *
 * 二、lambda表达式需要函数式接口的支持:
 *          若接口中只有一个抽象方法的接口,则为函数式接口,可使用注解 @FunctionalInterface,
 *          则可检查这个接口是否为函数式接口。
 *
 */
public class TestLambda {

    public static void main(String[] args) {
        final int num = 1;

        // 无参无返回值
        Runnable runnable = () -> System.out.println("hello Runnable"+num);
        Thread thread = new Thread(runnable);
        thread.start();

        // 有参数无返回值
        Consumer<String> consumer = s-> System.out.println(s);
        consumer.accept("Consumer");
        consumer.andThen(consumer);

        // 多个参数,lambda体中有多条语句,有返回值
        Comparator<Integer> comparator = (o1, o2)->{
              System.out.println(o1-o2);
              return Integer.compare(o1,o2);
        };

        // 若lambda体中只有一条语句,则return、{} 都可省
        Comparator<Integer> comparator1 = (o1, o2)->Integer.compare(o1,o2);

        // 对一个数进行运算
        Integer operator = operator(3, x -> x * x);
        System.out.println(operator);

    }

    public static Integer operator(Integer a,MyFuntion myFuntion){
        return myFuntion.getValue(a);
    }

}

Lambda表达式相关练习

@FunctionalInterface
public interface MyFuntion<String> {

    java.lang.String getValue(java.lang.String t);

}
@FunctionalInterface
public interface MyFuntion2<T,E> {

    E getValue(T t,T t2);

}
public class TestLambda {

    private static List<Employ> list = Arrays.asList(new Employ("张三",18,99999.9),
            new Employ("李四",18,3439.9),
            new Employ("王五",36,6666.9),
            new Employ("赵六",41,7777.9),
            new Employ("田七",9,2222.9));

    public static void main(String[] args) {
        Collections.sort(list,(o1, o2) -> {
            if (o1.getAge().intValue() == o2.getAge().intValue()){
                return o1.getSalary().compareTo(o2.getSalary());
            } else {
                return o1.getAge().compareTo(o2.getAge());
            }
        });

        list.stream().forEach(System.out::println);


        // 字符串去除空格
        String s = handlerString("    nihao   ", str -> str.trim());
        System.out.println(s);

        // 转大写
        String s1 = handlerString("hello world", str -> str.toUpperCase());
        System.out.println(s1);


        // 对于两个Long型数据进行处理
        Long aLong = handlerLong(100L, 200L, (x1, x2) -> x1 + x2);
        System.out.println(aLong);

    }

    // 对于两个Long型数据进行处理
    public static Long handlerLong(Long l1,Long l2,MyFuntion2<Long,Long> myFuntion){
        return myFuntion.getValue(l1,l2);
    }

    // 用于对字符串的处理
    public static String handlerString(String str, MyFuntion myFuntion){
        return myFuntion.getValue(str);
    }

}

相关文章

网友评论

      本文标题:Lambda表达式基础语法

      本文链接:https://www.haomeiwen.com/subject/ksqxpftx.html