没有用Lambda表达式的写法:

Comparator<Transaction> byYear = new Comparator<Transaction>() {
@Override public int compare(Transaction o1, Transaction o2) { return o1.getValue().compareTo(o2.getValue());
}
};

Lambda表达式的写法:

 
Comparator<Transaction> byYear = (o1, o2) -> o1.getValue().compareTo(o2.getValue());

Lambda表达式的三部分:

  1. 参数列表  这里采用了Comparator中compare的方法的参数,两个Transaction

  2. 箭头     箭头->把参数列表和Lambda主体分隔开.

  3. Lambda主体   比较两个Transaction的年份,表达式就是Lambda的返回值

下面提供了一些Lambda的例子和使用案例

 
(List<String> list) -> list.isEmpty();
() -> new Transaction();
(Transaction t) -> {
System.out.println("Year = " + t.getYear());
}
(String s) -> s.length();
(int a, int b) -> a * b;

在哪里使用Lambda接口

  可以在函数式接口上使用Lambda表达式,

  1. 函数式接口 就是只定义了一个抽象方法的接口,继承的也不行 作用: 是函数式接口一个具体的实现实例

例子:

    
Runnable r2 = new Runnable() {//使用匿名类            @Override            public void run() {
System.out.println("hello world 2");
}
};
process(r1);//打印hello world 1
process(r2);//打印hello world 2
process(()-> System.out.println("hello world 3"));//利用直接传递Lambda表达式作为实现实例 打印hello world 3
  如果我们去看看新的java Api,会发现函数式接口带有@FunctionalInterface标注,这个标注表示该接口被设计成一个函数式接口,如果你用了该注解,而它不是却不是函数式接口,编译器会返回一个提示错误.
/**
* The <code>Runnable</code> interface should be implemented by any
* class whose instances are intended to be executed by a thread. The
* class must define a method of no arguments called <code>run</code>.
* <p>
* This interface is designed to provide a common protocol for objects that
* wish to execute code while they are active. For example,
* <code>Runnable</code> is implemented by class <code>Thread</code>.
* Being active simply means that a thread has been started and has not
* yet been stopped.
* <p>
* In addition, <code>Runnable</code> provides the means for a class to be
* active while not subclassing <code>Thread</code>. A class that implements
* <code>Runnable</code> can run without subclassing <code>Thread</code>
* by instantiating a <code>Thread</code> instance and passing itself in
* as the target. In most cases, the <code>Runnable</code> interface should
* be used if you are only planning to override the <code>run()</code>
* method and no other <code>Thread</code> methods.
* This is important because classes should not be subclassed
* unless the programmer intends on modifying or enhancing the fundamental
* behavior of the class.
*
* @author Arthur van Hoff
* @see java.lang.Thread
* @see java.util.concurrent.Callable
* @since JDK1.0 */@FunctionalInterfacepublic interface Runnable { /**
* When an object implementing interface <code>Runnable</code> is used
* to create a thread, starting the thread causes the object's
* <code>run</code> method to be called in that separately executing
* thread.
* <p>
* The general contract of the method <code>run</code> is that it may
* take any action whatsoever.
*
* @see java.lang.Thread#run() */
public abstract void run();
}

方法引用

方法引用让你可以重复使用现有的方法定义,并像Lambda一样传递他们.例如下面排序的例子:
List<Apple> inventory = new ArrayList<>();
inventory.sort(Comparator.comparing(Apple::getWeight)//按重量排序
.reversed()//递减 倒序
.thenComparing(Apple::getCountry));//如果重量一样 按国家排序

当你需要使用方法引用时,目标引用放在分隔符::前,方法名称放在后面,例如Apple::getWeight 就是引用了Apple类中定义的getWeight,请记住,不需要括号,因为你没有实际调用这个方法.

方法引用主要有三类:

  1. 指向静态方法的方法引用(例如Integer的parseInt方法 写作Integer::parseInt) .

  2. 指向任意类型实例方法的方法引用(例如String的length方法,写作String::length)

  3. 指向现有对象的实例方法的方法引用(假设你有一个局部变量expensive用于存放Transaction类型的对象,他支持实例方法getValue,那么你就可以写expensive::getValue).

构造函数引用

  对于一个现有构造函数,你可以利用它的名称和关键字new 来创建他的一个引用:ClassName::new,它的功能与指向静态方法的引用类型类似.

 

Java8新特性学习笔记(一) Lambda表达式的更多相关文章

  1. java8 新特性学习笔记

    Java8新特性 学习笔记 1主要内容 Lambda 表达式 函数式接口 方法引用与构造器引用 Stream API 接口中的默认方法与静态方法 新时间日期 API 其他新特性 2 简洁 速度更快 修 ...

  2. Java8新特性(一)——Lambda表达式与函数式接口

    一.Java8新特性概述 1.Lambda 表达式 2. 函数式接口 3. 方法引用与构造器引用 4. Stream API 5. 接口中的默认方法与静态方法 6. 新时间日期 API 7. 其他新特 ...

  3. Java8新特性第1章(Lambda表达式)

    在介绍Lambda表达式之前,我们先来看只有单个方法的Interface(通常我们称之为回调接口): public interface OnClickListener { void onClick(V ...

  4. java8新特性学习笔记(二) 使用流(各种API)

    筛选和切片 用谓词筛选,筛选出各个不相同的元素,忽略流中的头几个元素,或将流截断至指定长度 用谓词筛选 Stream接口支持filter方法,该操作接受一个谓词(返回一个boolean的函数) 作为参 ...

  5. Java8新特性 利用流和Lambda表达式对List集合进行处理

    Lambda表达式处理List 最近在做项目的过程中经常会接触到 lambda 表达式,随后发现它基本上可以替代所有 for 循环,包括增强for循环.也就是我认为,绝大部分的for循环都可以用 la ...

  6. java8新特性学习笔记(二) 流的相关思想

    流是什么 流是Java API的新成员,他允许你以声明的方式处理数据集合,就现在来说,可以把他们看成遍历数据集合的高级迭代器.此外,流还可以透明地并行处理,你无须写任何多线程代码. 下面例子是新老AP ...

  7. java8新特性学习笔记链接

    https://blog.csdn.net/yitian_66/article/details/81010434

  8. Java8 新特性学习 Lambda表达式 和 Stream 用法案例

    Java8 新特性学习 Lambda表达式 和 Stream 用法案例 学习参考文章: https://www.cnblogs.com/coprince/p/8692972.html 1.使用lamb ...

  9. Java1.8新特性——接口改动和Lambda表达式

    Java1.8新特性——接口改动和Lambda表达式 摘要:本文主要学习了Java1.8的新特性中有关接口和Lambda表达式的部分. 部分内容来自以下博客: https://www.cnblogs. ...

随机推荐

  1. Tomcat域名映射和端口设置

    1.打开tomcat主目录  --->  打开conf目录 ---> 找到并打开server.xml文件 2.修改tomcat的监听端口为80端口 在文件中找到: <Connecto ...

  2. 什么是http头信息

    HTTP(HyperTextTransferProtocol)是超文本传输协议的缩写,它用于传送WWW方式的数据,Http协议定义了很多与服务器交互的方法,最基本的有4种,分别是GET.POST.PU ...

  3. PHP中的各种POST或GET请求

    1.表单<form> 2.fsockopen(); 3.jquery语法: $.post ($.ajax的简写) 示例代码: <!DOCTYPE html> <html& ...

  4. C++中public、protected以及private的使用

    相比C语言,C++中通过class/struct来定义既包含数据,又包含行为的结构,从而支持了“对象”.现实世界中,一个人(一个对象)通常 拥有一些资产(数据),并且掌握某些技能(行为),并且这些资产 ...

  5. Ninject在.NET WebForm和MVC中的使用

    1.建立项目:Models/BLL/DAL/IBLL/IDAL/WebSite 2.WebSite要引用其余几个项目 3.NuGet中搜索Ninject,安装 Ninject.Ninject.Web. ...

  6. 前端基础之:JQuery(可编辑版)

     前端基础之jquery   一 jQuery是什么? [1]   jQuery由美国人John Resig创建,至今已吸引了来自世界各地的众多 javascript高手加入其team. [2]   ...

  7. linux每天一小步---mkdir命令详解

    1 命令功能 mkdir命令用于创建单个目录或者多级目录,但前提在于用户对于当前目录有写权限. 2 命令语法 mkdir  [选项]  [目录名] 3 命令参数 -m 在创建目录的同时设定目录权限(而 ...

  8. UIWebview与js交互[转]

    UIWebView是iOS最常用的SDK之一,它有一个stringByEvaluatingJavaScriptFromString方法可以将javascript嵌入页面中,通过这个方法我们可以在iOS ...

  9. ZOJ2256 Mincost 2017-04-16 19:36 44人阅读 评论(0) 收藏

    Mincost Time Limit: 2 Seconds      Memory Limit: 65536 KB The cost of taking a taxi in Hangzhou is n ...

  10. mysql数据表简单拷贝及重命名

    CREATE TABLE to LIKE from;//拷贝结构 RENAME TABLE from TO to;//重命名