Java新特性(一) Lambda表达式教程
大纲 新特性有哪些:
总结点:
- 速度更快 – 红黑树
- 代码更少 – Lambda
- 强大的Stream API – Stream
- 便于并行 – Parallel
- 最大化减少空指针异常 – Optional
仔细的读这篇文章,让你快速掌握Lambda的使用.
package com.java8.peculiarity;
import com.java8.entity.Persion;
import com.java8.interfaces.FunctionInterFace;
import com.java8.interfaces.PredicateLambda;
import org.junit.Test;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
/**
* @author misterWei
* @create 2018年10月23号:13点53分
* @mailbox [email protected]
* <p>
* 基础语法Lambda的说明:
* Java8 出现了一个新的操作符即箭头操作符 -> 也称为Lambda操作符
* Lambda操作符分为两部分 即 左侧 右侧
* (参数列表)->(Lambda体)
* 左侧: 参数列表
* 右侧 Lambda体,所实现的功能
*/
public class LambdaDemo {
/**
* 接口中的静态方法使用
*/
@Test
public void interfaceTest() {
//调用接口中的静态方法
String name = PredicateLambda.returnString("张三");
System.out.println(name);
//使用表达式对数据进行操作
PredicateLambda predicateLambda = (x, y) -> {
return x + y;
};
Integer integer = predicateLambda.returnInt(4, 6);
System.out.println(integer);
}
/**
* 语法格式一,无参数,无返回值
*/
@Test
public void testLambda() {
//首先看看我们以前是怎么实现的
int i = 0;
Runnable runnable = new Runnable() {
public void run() { //引用局部变量也不需要加final了,但是底层还是final
System.out.println("我是匿名内部类!" + i);
}
};
runnable.run();
System.out.println("**************************************");
//Lambda怎么实现的
Runnable runnable1 = () -> System.out.println("我是表达式实现的");
runnable1.run();
}
/**
* 语法格式二, 有参数,无返回值
* 如果参数只有一个那么括号可以省略不写
*/
@Test
public void testLambda2() {
//回调函数
Consumer consumer = (x) -> System.out.println(x);
consumer.accept("张三");
//省略小括号
Consumer consumer2 = x -> System.out.println(x);
consumer2.accept("李四");
}
/**
* 语法格式三,多个参数并且还有返回值
* 需要使用{
* return 逻辑代码;
* };
*/
@Test
public void testLambda3() {
Comparable comparable = (x) -> {
//>
return Integer.compare((Integer) x, 50);
};
int i = comparable.compareTo(30);
System.out.println(i);
//补充说明,如果只有一条语句 return 那么{},return 可以省略
Comparable comparable1 = (x) -> Integer.compare((Integer) x, 50);
int i2 = comparable.compareTo(30);
System.out.println(i2);
}
/**
* 语法格式四: 所使用的表达式接口必须式函数式接口
* : 什么是函数接口: 一个接口中只有一个抽象方法 参照 interfaces包下的函数接口(FunctionInterFace)
*/
@Test
public void testLambda4() {
//基于函数回调的操作
FunctionInterFace functionInterFace = x -> x * x;
Integer integer = functionInterFace.numberMath(5);
System.out.println(integer);
}
/**
* 使用Lambda继续集合排序操作
*/
public List<Persion> list = Arrays.asList(
new Persion("张三", 24, new BigDecimal(55.66d)),
new Persion("李四", 29, new BigDecimal(66.66d)),
new Persion("赵六", 18, new BigDecimal(77.77d)),
new Persion("田七", 32, new BigDecimal(55.44d)),
new Persion("李丹", 34, new BigDecimal(88.66d))
);//排序操作
@Test
public void testLambda5() {
Collections.sort(list, (x, y) -> {
if (x.getUserName().equals(y.getUserName())) {
return x.getUserName().compareTo(y.getUserName());
} else {
return -x.getAge().compareTo(y.getAge());
}
});
for (Persion persion : list) {
System.out.println(persion);
}
}
@Test
public void testLambda6() {
FunctionInterFace functionInterFace = (x) -> x+x;
Integer integer = functionInterFace.numberMath(15);
System.out.println(integer);
}
}
我的函数接口
Package com.java8.interfaces;
/**
* @author misterWei
* @create 2018年10月23号:22点12分
* @mailbox [email protected]
*/
@FunctionalInterface//函数接口的注解定义 (只允许有一个抽象方法)
public interface FunctionInterFace {
public Integer numberMath(Integer num);
}
我的接口方法体表示
package com.java8.interfaces;
/**
* @author misterWei
* @create 2018年10月23号:17点01分
* @mailbox [email protected]
*
* 1.8 使劲造,接口中定义方法
*
* 满足两点 static修饰 或者default修饰
*/
public interface PredicateLambda {
//示例:
static String returnString(String name){
return name;
}
public Integer returnInt(Integer sex,Integer gender);
}
**我的Pojo**
package com.java8.entity;
import java.io.Serializable;
import java.math.BigDecimal;
/**
* @author misterWei
* @create 2018年10月23号:22点31分
* @mailbox [email protected]
*/
public class Persion implements Serializable {
private String userName;
private Integer age;
private BigDecimal money;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Persion(String userName, Integer age, BigDecimal money) {
this.userName = userName;
this.age = age;
this.money = money;
}
public BigDecimal getMoney() {
return money;
}
public void setMoney(BigDecimal money) {
this.money = money;
}
@Override
public String toString() {
return "Persion{" +
"userName='" + userName + '\'' +
", age=" + age +
", money=" + money +
'}';
}
}
持续更新1.8新特性 (代码在github中)github点击连接