Jdk8中有好多新的特性,比如引入Lambda,简化代码的书写等等

我们先看一个关于Lambda的使用

/**
* 输出list
*/
@Test
public void test() {
String[] array = {"aaaa", "bbbb", "cccc"};
List<String> list = Arrays.asList(array);

//Java 7
for(String s:list){
System.out.println(s);
}

//Java 8
list.forEach(System.out::println);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
其中list.forEach(System.out::println);就是Java 8中的Lambda写法之一, 有没有特别注意到输出语句跟我们平时写的syso语句不一样,常规输出语句是这样的:

System.out.println("流浪地球拍的不错哦!");
1
这里面使用到了::, 有点意思,来认识一下这个新东西!

双冒号(::)
英文:double colon,双冒号(::)运算符在Java 8中被用作方法引用(method reference),方法引用是与lambda表达式相关的一个重要特性。它提供了一种不执行方法的方法。为此,方法引用需要由兼容的函数接口组成的目标类型上下文。

Method References
You use lambda expressions to create anonymous methods. Sometimes, however, a lambda expression does nothing but call an existing method. In those cases, it’s often clearer to refer to the existing method by name. Method references enable you to do this; they are compact, easy-to-read lambda expressions for methods that already have a name.
关于方法引用的描述,摘自oracle官网

大致意思是,使用lambda表达式会创建匿名方法, 但有时候需要使用一个lambda表达式只调用一个已经存在的方法(不做其它), 所以这才有了方法引用!

以下是Java 8中方法引用的一些语法:

静态方法引用(static method)语法:classname::methodname 例如:Person::getAge
对象的实例方法引用语法:instancename::methodname 例如:System.out::println
对象的超类方法引用语法: super::methodname
类构造器引用语法: classname::new 例如:ArrayList::new
数组构造器引用语法: typename[]::new 例如: String[]:new
如果上的语法太枯燥,那就通过一些例子来加强对它的理解:

静态方法语法使用例子:

import java.util.Arrays;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
*
* @author zhoufy
* @date 2019年2月20日 下午2:19:13
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ComponentScan("com.zhoufy")
public class Demo {
@Test
public void test() {
List<String> list = Arrays.asList("aaaa", "bbbb", "cccc");

//静态方法语法 ClassName::methodName
list.forEach(Demo::print);
}

public static void print(String content){
System.out.println(content);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
类实例方法语法使用例子:

import java.util.Arrays;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
*
* @author zhoufy
* @date 2019年2月20日 下午2:19:13
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ComponentScan("com.zhoufy")
public class Demo {
@Test
public void test() {
List<String> list = Arrays.asList("aaaa", "bbbb", "cccc");

//对象实例语法 instanceRef::methodName
list.forEach(new Demo()::print);
}

public void print(String content){
System.out.println(content);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
超类方法语法使用例子:

import java.util.Arrays;
import java.util.List;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
* @author zhoufy
* @date 2019年2月20日 下午2:41:38
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ComponentScan("com.zhoufy")
public class Example extends BaseExample{

@Test
public void test() {
List<String> list = Arrays.asList("aaaa", "bbbb", "cccc");

//对象的超类方法语法: super::methodName
list.forEach(super::print);
}
}

class BaseExample {
public void print(String content){
System.out.println(content);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
类构造器语法使用例子:

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
*
* @author zhoufy
* @date 2019年2月20日 下午2:19:13
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ComponentScan("com.zhoufy")
public class Example {

@Test
public void test() {
InterfaceExample com = Example::new;
Example bean = com.create();
System.out.println(bean);
}
}

interface InterfaceExample{
Example create();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
如果是带参数的构造器,示例如下:

/**
* @author zhoufy
* @date 2019年2月20日 下午2:19:13
*/
public class Example {

private String name;

Example(String name){
this.name = name;
}

public static void main(String[] args) {
InterfaceExample com = Example::new;
Example bean = com.create("hello world");
System.out.println(bean.name);
}
}
interface InterfaceExample{
Example create(String name);
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
这里需要特别注意的是:Example 类并没有implements InterfaceExample接口哦!!!

数组构造器语法使用例子:

import java.util.function.Function;

/**
* @author zhoufy
* @date 2019年2月20日 下午2:19:13
*/
public class Example {
public static void main(String[] args) {
Function <Integer, Example[]> function = Example[]::new;
Example[] array = function.apply(4); //这里的4是数组的大小

for(Example e:array){
System.out.println(e); //如果输出的话,你会发现会输出4个空对象(null)
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
这里是借用jdk自带的java.util.function.Function类实现的,如果想要自定义接口

/**
*
* @author zhoufy
* @date 2019年2月20日 下午2:19:13
*/
public class Example {

public static void main(String[] args) {
Interface <Integer, Example[]> function = Example[]::new;
Example[] array = function.apply(4); //这里的4是数组的大小

for(Example e:array){
System.out.println(e);
}
}
}

@FunctionalInterface
interface Interface<A, T>{
T apply(A a);
}
---------------------
作者:扬帆舟
来源:CSDN
原文:https://blog.csdn.net/zhoufanyang_china/article/details/87798829
版权声明:本文为博主原创文章,转载请附上博文链接!

jdk8新特性表达式1的更多相关文章

  1. JDK8新特性一览

    转载自:http://blog.csdn.net/qiubabin/article/details/70256683 官方新特性说明地址 Jdk8新特性.png 下面对几个常用的特性做下重点说明. 一 ...

  2. 一次电话Java面试的问题总结(JDK8新特性、哈希冲突、HashMap原理、线程安全、Linux查询命令、Hadoop节点)

    面试涉及问题含有: Java JDK8新特性 集合(哈希冲突.HashMap的原理.自动排序的集合TreeSet) 多线程安全问题 String和StringBuffer JVM 原理.运行流程.内部 ...

  3. 深入理解java虚拟机---jdk8新特性(二)

    1.jdk8新特性 1.新特性 2.lambda函数表达式的作用 A: 替换内部类 B:对集合的操作并行化

  4. JDK8新特性:使用stream、Comparator和Method Reference实现集合的优雅排序

    大家对java接口Comparator和Comparable都不陌生,JDK8里面Comparable还和以前一样,没有什么改动:但是Comparator在之前基础上增加了很多static和defau ...

  5. JDK8 新特性

    JDK8 新特性目录导航: Lambda 表达式 函数式接口 方法引用.构造器引用和数组引用 接口支持默认方法和静态方法 Stream API 增强类型推断 新的日期时间 API Optional 类 ...

  6. JDK8新特性关于Stream流

    在Java1.8之前还没有stream流式算法的时候,我们要是在一个放有多个User对象的list集合中,将每个User对象的主键ID取出,组合成一个新的集合,首先想到的肯定是遍历,如下: 1 2 3 ...

  7. java进阶一之jdk8新特性

    1.官方发布的jdk8新特性 2.51CTO相关专题

  8. jdk8新特性:在用Repository实体查询是总是提示要java.util.Optional, 原 Inferred type 'S' for type parameter 'S' is not within its bound;

    jdk8新特性:在用Repository实体查询是总是提示要java.util.Optional 在使用springboot 方法报错: Inferred type 'S' for type para ...

  9. jdk8新特性

    JDK8新特性(JDK8的新特性) * 接口中可以定义有方法体的方法,如果是非静态,必须用default修饰 * 如果是静态的就不用了 class Test { public void run() { ...

随机推荐

  1. spring jdbcTemplate 事务,各种诡异,包你醍醐灌顶!

    前言 项目框架主要是spring,持久层框架没有用mybtis,用的是spring 的jdbc: 业务需求:给应用添加领域(一个领域包含多个应用,一个应用可能属于多个领域,一般而言一个应用只属于一个领 ...

  2. 分布式系统监视zabbix讲解一之zabbix安装--技术流ken

    zabbix概述 Zabbix是什么 Zabbix 是由Alexei Vladishev创建,目前由Zabbix SIA在持续开发和支持. Zabbix 是一个企业级的分布式开源监控方案. Zabbi ...

  3. 业务开发(八)—— Maven

    0x01.maven install成功后,项目旁边依然有个红叉 maven--update--选中下方Force Update of Snapsshots/Releases 0x02.An inte ...

  4. ADO.NET基础学习 一(连接数据库)

    (记录下方便自己复习) 概念 简单地理解为用来连接数据库的类. 工作流程 ①Connection对象用来连接数据库. 两种连接方式:Windows身份验证 / sqlserver验证 private ...

  5. 数据库 'xxxx' 的事务日志已满。若要查明无法重用日志中的空间的原因

    一.出现的背景: 在SQL server中执行SQL语句出现如下图: 二.出现的原因: 我到数据库的服务器看了一下硬盘空间发现此数据库所在的D盘空间几乎已经用尽.如图: 三.解决方法: 第一种方法:直 ...

  6. Message": "请求的资源不支持 http 方法“GET”

    今天用postman测试后端api,总是报错,下面是问题解决方案. 一.测试方法 public ApiResult Get(int id){ApiResult result = new ApiResu ...

  7. How to Create a First Shell Script

    How to Create a First Shell Script   Shell scripts are short programs that are written in a shell pr ...

  8. java安全管理器SecurityManager介绍

    java安全管理器类SecurityManager简单剖析: javadoc介绍: SecurityManager是一个允许应用实现一种安全策略的类.它允许一个应用去明确,在执行一个可能安全或者敏感的 ...

  9. 年会抽奖 抽奖系统 抽奖软件 C# Winform

    年会抽奖软件: Q.Q 358189777 C#.  数据库Access: 1.系统启动,自动全屏展示. 2.背景随心切换. 3.快捷键方便自如: F1:弹出设置界面 F2:查询人员名单.中奖名单 F ...

  10. C#导入Excel、Excel导入、导入.xls 、导入.xlsx、Excel2003版本、Excel2007版本

    C#导入Excel: 1.选择Excel 03版文件 2.选择需要读取数据的Excel工作表   3.选择工作表中需要读取的列 源码地址在图片下面,不要点击图片,点击下载地址跳转下载.