lambda语法糖入门

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hlw521hxq/article/details/86480644

Lambda函数的使用

lambda是基于函数式编程,常用的最常见的函数式编程

new Thread(() ->System.out.println("out")).start();

参考博客内容:(找不到作者地址,有的话,请联系我,我添加上)

        //函数签名(T t) -> boolean
        Predicate<String> predicate = s -> s == null|s==" ";

		//函数签名(T t) -> void
        Consumer<String> consumer = s -> System.out.println(s);

		//函数签名(T t) -> R
		//方法名function可能会与main方法的function同名,建议测试的时候换名字
        Function<String, Integer> function = s -> s.length();
        
		//函数签名() -> T
        Supplier<String> supplier = () -> "I'am sevin";

你要是没有看懂的话,我给你解释下

  1. 用于返回布尔值判断的函数编程语法
    	 System.out.println(predicate.test(" "));//预期结果true
    
  2. 用于void方法的匿名函数
     		consumer.accept("世界末日");//预期“”世界末日
    
  3. 用于返回制定值的语法
    	    System.out.println(function.apply("124"));//预期3
    
  4. 用户返回泛型的语法
    	 System.out.println(supplier.get());//预期“I'm sevin”
    

其他常用函数

  • Thread
  • CompareTo
  • forEach

扩展连接:

  • 语法树

猜你喜欢

转载自blog.csdn.net/hlw521hxq/article/details/86480644