Java1.8-Lambda

写的很全很好的一个博客: http://blog.csdn.net/ioriogami/article/details/12782141

什么是函数接口: http://www.lambdafaq.org/what-is-a-functional-interface/ 肤浅的理解就是:函数注解的接口是这样一个类型,这个类型的作用相当于一个函数

单看这两个博客理解起来有些吃力,看看java1.8中对FounctionalInterface的解释:

/*
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
 * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package java.lang;

import java.lang.annotation.*;

/**
 * An informative annotation type used to indicate that an interface
 * type declaration is intended to be a <i>functional interface</i> as
 * defined by the Java Language Specification.
 * :functionalInterface注解用来指明一个接口类型是functional interface,这句话好像没什么用
 * Conceptually, a functional interface has exactly one abstract
 * method.  Since {@linkplain java.lang.reflect.Method#isDefault()
 * default methods} have an implementation, they are not abstract.  If
 * an interface declares an abstract method overriding one of the
 * public methods of {@code java.lang.Object}, that also does
 * <em>not</em> count toward the interface‘s abstract method count
 * since any implementation of the interface will have an
 * implementation from {@code java.lang.Object} or elsewhere.
 *:一个函数注解借口准确的说,只有一个抽象方法,且这个方法不能是重写的Object的public方法(不计入does not count toward)
 * <p>Note that instances of functional interfaces can be created with
 * lambda expressions, method references, or constructor references.
 *:一个函数注解借口可以使lambda表达式、方法的引用、或者构造函数的引用
 * <p>If a type is annotated with this annotation type, compilers are
 * required to generate an error message unless:
 *:使用这个注解的时候,为了使编译器不报错,至少要满足:
 * <ul>
 * <li> The type is an interface type and not an annotation type, enum, or class.
 * <li> The annotated type satisfies the requirements of a functional interface.
 * </ul>
 *:type是借口而不是注解、枚举、类
 * <p>However, the compiler will treat any interface meeting the
 * definition of a functional interface as a functional interface
 * regardless of whether or not a {@code FunctionalInterface}
 * annotation is present on the interface declaration.
 *:这里描述没用使用functionalInterface注解却被认作是函数注解接口的情况:meeting the definition of a functional interface即满足了函数接口的定义
 * @jls 4.3.2. The Class Object
 * @jls 9.8 Functional Interfaces
 * @jls 9.4.3 Interface Method Body
 * @since 1.8
 */
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface FunctionalInterface {}

简单的例子:

Runnable接口早就有了,FunctionalInterface是1.8才出的,Runnable接口只有一个抽象函数,满足FounctionalInterface的定义,属于上面提到的特殊情况,在1.8中加上了@FunctionalInterface注解

Thread th=new Thread(()->System.out.println("hello"));
        th.start();
时间: 2024-08-10 11:20:41

Java1.8-Lambda的相关文章

java1.8 lambda进行并行运算

parallelStream()支持并行运算: package com.roocon.thread.t2; import java.util.Arrays; import java.util.List; public class Demo7 { public static void main(String[] args) { List<String> values = Arrays.asList("1","2","3","4

java1.8新特性(一)

一直在更新java 版本,原来也没有关注java版本的变化 引入的一些新的api  引起注意的还是  关于一些并发包的使用,那时候才对每个版本的特性 去了解了一下,虽然 不一定都用上了,但是不管学习什么语言,花点时间学习新的api还是很有必要的,在工作中 还用一些 1.8的新特性,但是 始终 是 不明白.总是有问题,特意花些时间 去重点学习一下,如果其中 有什么说的不对,请一定要提出来! 用户 对象 User: package com.java; public class User { Stri

java1.8中Lambda表达式reduce聚合测试例子

public class LambdaTest { public static void main(String[] args) { // 相当于foreach遍历操作结果值 Integer out = Stream.of(10, 5, 3, 2, 1, 0).reduce((result, item) -> { if (item >= 3) { result = result + item; } return result; }).get(); System.out.println(out)

一分钟搞明白java8中的lambda

项目结构是这样的 User是一个普通的pojo类 UserCompare是一个实现了Comprator的类 现在我们有一个需求:给一个user组成的list 按照user的年龄排序.实现不难,代码如下: 这种方法由于sort方法的第二个参数是Comparator 所以你要写一个实现类(我这里是UserCompare类),并且override该接口的实现方法. java8提供了lambda来简化,有了lambda程序员从此不加班呀- 刚才那个Comparator的实现类以及内部若干代码就都省了,代

jdk1.8新特性之lambda表达式及在Android Studio中的使用举例

Jdk1.8已经出很久了但是很多同学对它的特性在android studio 中的应用可能还不是很熟悉,今天我们就来对这个新特性在AS中做它的应用实践. 一.首先在有JDK1.8的情况下我们要在AS的设置中将这个地方设置成java1.8的. 二在build.gradle中添加上这两句 即: compileOptions {    sourceCompatibility 1.8    targetCompatibility 1.8} 这样就完成了使用之前的设置工作了.然后我们进入重要的应用. 那么

自己理解Java中的lambda

lambda是什么 "Lambda 表达式"(lambda expression)是一个匿名函数,Lambda表达式基于数学中的λ演算得名,直接对应于其中的lambda抽象(lambda abstraction),是一个匿名函数,即没有函数名的函数.Lambda表达式可以表示闭包(注意和数学传统意义上的不同). 将一个变量传入一个匿名函数然后对传入函数进行操作.由于java中并没有脱离类而存在的函数,所以通常独立函数是以一个匿名内部类+一个方法构成的.lambda表达式代替的函数既没有

java8新特性——Lambda表达式

上文中简单介绍了一下java8得一些新特性,与优点,也是为本次学习java8新特性制定一个学习的方向,后面几篇会根据上文中得新特性一一展开学习.本文就从java8新特性中比较重要的Lambda表达式开始学学习. 一.为什么要使用Lambda表达式 Lambda是一个匿名函数,我们可以baLambda表达式理解为是一段可以传递的代码(将代码像数据一样进行传递).可以写出更简洁,更灵活的代码.作为一种更紧凑得代码风格,使得java得语言表达能力得到提升.Lambda表达式需要函数式接口的支持,接口中

Lambda表达式实战视频教程

视频教程地址: https://my.oschina.net/u/3217554/blog/1507456 1:Lambda表达式及函数式接口介绍 2:Lambda表达式详解 3:方法的引用(一) 4:方法的引用(二) 5:Stream API(一) 6:Stream API(二) 7:Lambda表达式.方法的引用.Stream API实战

lambda表达式封装对数据库的查询

前言: 1.为什么要封装lambda表达式数据库查询,原因有一下几点: 1.1.在以往的开发中进行数据库表查询时,其实所需要的字段就是其中几个,但是在开发中,开发者往往习惯select * 进行查询,当数据多和用户量多时,查询的效率会降低. 1.2.在写查询where条件的时候,总是用string.format去拼接字符串,开发效率低. 1.3.代码不够优雅,代码中嵌套和多sql语句,如果是表字段发生改变时编译器检查不出来,代码出错的概率大. 1.4.本着 write less  do more