package com.atguigu.java8;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer; import org.junit.Test; /*
* 一、Lambda 表达式的基础语法:Java8中引入了一个新的操作符 "->" 该操作符称为箭头操作符或 Lambda 操作符
* 箭头操作符将 Lambda 表达式拆分成两部分:
*
* 左侧:Lambda 表达式的参数列表
* 右侧:Lambda 表达式中所需执行的功能, 即 Lambda 体
*
* 语法格式一:无参数,无返回值
* () -> System.out.println("Hello Lambda!");
*
* 语法格式二:有一个参数,并且无返回值
* (x) -> System.out.println(x)
*
* 语法格式三:若只有一个参数,小括号可以省略不写
* x -> System.out.println(x)
*
* 语法格式四:有两个以上的参数,有返回值,并且 Lambda 体中有多条语句
* Comparator<Integer> com = (x, y) -> {
* System.out.println("函数式接口");
* return Integer.compare(x, y);
* };
*
* 语法格式五:若 Lambda 体中只有一条语句, return 和 大括号都可以省略不写
* Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
*
* 语法格式六:Lambda 表达式的参数列表的数据类型可以省略不写,因为JVM编译器通过上下文推断出,数据类型,即“类型推断”
* (Integer x, Integer y) -> Integer.compare(x, y);
*
* 上联:左右遇一括号省
* 下联:左侧推断类型省
* 横批:能省则省
*
* 二、Lambda 表达式需要“函数式接口”的支持
* 函数式接口:接口中只有一个抽象方法的接口,称为函数式接口。 可以使用注解 @FunctionalInterface 修饰
* 可以检查是否是函数式接口
*/
public class TestLambda2 { @Test
public void test1(){
int num = ;//jdk 1.7 前,必须是 final Runnable r = new Runnable() {
@Override
public void run() {
System.out.println("Hello World!" + num);
}
}; r.run(); System.out.println("-------------------------------"); Runnable r1 = () -> System.out.println("Hello Lambda!");
r1.run();
} @Test
public void test2(){
Consumer<String> con = x -> System.out.println(x);
con.accept("我大尚硅谷威武!");
} @Test
public void test3(){
Comparator<Integer> com = (x, y) -> {
System.out.println("函数式接口");
return Integer.compare(x, y);
};
} @Test
public void test4(){
Comparator<Integer> com = (x, y) -> Integer.compare(x, y);
} @Test
public void test5(){
// String[] strs;
// strs = {"aaa", "bbb", "ccc"}; List<String> list = new ArrayList<>(); show(new HashMap<>());
} public void show(Map<String, Integer> map){ } //需求:对一个数进行运算
@Test
public void test6(){
Integer num = operation(, (x) -> x * x);
System.out.println(num); System.out.println(operation(, (y) -> y + ));
} public Integer operation(Integer num, MyFun mf){
return mf.getValue(num);
}
}

以上资料仅供学习使用

最新文章

  1. [Asp.net 5] Caching-缓存预告
  2. 天河2号荣膺第41届TOP500榜首
  3. Java(五)
  4. document.images、document.forms、doucument.links——&gt;HTMLCollection
  5. celery简单应用
  6. 实用的ajaxfileupload插件
  7. python实现插入排序
  8. 解决UITableView头部空白
  9. MYSQL查询男女数量的存储过程
  10. NSIS:检查某注册表键是否存在
  11. Python字典(dict)使用技巧
  12. 企业微信快捷接入Odoo的模块——WeOdoo
  13. 【资源分享】ArcFace Demo [Android]
  14. Linux----Github环境搭建
  15. HttpClient 通过代理访问验证服务器
  16. faster rcnn讲解很细
  17. 《你不知道的javascript》读书笔记1
  18. P2325 [SCOI2005]王室联邦 解题报告
  19. Git进行fork后如何与原仓库同步
  20. 微信小程序 - 获取所在位置(省、市、区)

热门文章

  1. 将数据从数据仓库Hive导入到MySQL
  2. dos 下小tip
  3. 【转】Spark:Master High Availability(HA)高可用配置的2种实现
  4. c# 线程浅析(代理 、Invoke、Lock)
  5. myBatis逆向生成及使用
  6. LN : leetcode 529 Minesweeper
  7. IE 浏览器在地址栏输入中文字符,发送get请求报400错误的问题
  8. tp在页面输出时间
  9. Android - 收藏集
  10. 编写Java脚本统计工程代码总行数