java8新特性(Lambda表达式)
1.速度更快(对于数据结构的变动以及内存优化)2.代码更少(Lambda表达式:匿名函数,一段可以传递的代码)3.强大的Stream API4.便于并行5.最大化减少空指针异常OptionalLambda表达式为什么使用?对于重复的业务代码进行条件更新时,会有大量的冗余,采用Lambda会简化代码例如:package com.Lambda;import org.junit.jupiter.api.
·
1.速度更快(对于数据结构的变动以及内存优化)
2.代码更少(Lambda表达式:匿名函数,一段可以传递的代码)
3.强大的Stream API
4.便于并行
5.最大化减少空指针异常Optional
Lambda表达式
为什么使用?
对于重复的业务代码进行条件更新时,会有大量的冗余,采用Lambda会简化代码
例如:
package com.Lambda;
import org.junit.jupiter.api.Test;
import java.util.*;
public class TestLambda {
public void test1(){
Comparator<Integer> com=new Comparator<Integer>(){
@Override
public int compare(Integer o1, Integer o2) {
return Integer.compare(o1,o2);
}
};
TreeSet<Integer>ts=new TreeSet<>(com);
}
-------------------------------------------------------------
//lambda表达式
public void test2(){
Comparator<Integer> com=(x,y)->Integer.compare(x,y);
TreeSet<Integer>ts=new TreeSet<>(com);
}
------传统的筛选需要根据筛选的要求进行代码迭代------
List<Employee> employees= Arrays.asList(
new Employee("张三",18,999.55),
new Employee("李四",45,499.55),
new Employee("王五",36,599.55),
new Employee("赵六",18,699.55),
new Employee("张七",17,799.55),
new Employee("田八",35,899.55)
);
@Test
public void test3(){
List<Employee> list=filterEmployees(employees);
for(Employee employee:list){
System.out.print(employee);
}
}
public List<Employee> filterEmployees(List<Employee> list){
List<Employee> emps=new ArrayList<>();
for(Employee emp:list){
if(emp.getAge()>=35){
emps.add(emp);
}
}
return emps;
}
}
----------------Lambda表达式表示工资小于5000------------------
List<Employee> list=filterEmployees(employees,(e) ->e.getSalary()<=5000);
1.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);
public class TestLambda2 {
@Test
public void test1(){
final int num=0;//jdk1.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!"+num);
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(){
Comparator<Integer> com=(x,y)->Integer.compare(x,y);
}
}
2.Lambda表达式需要"函数式接口"的支持
函数式接口:
- 接口中只有一个抽象方法的接口,成为函数式接口.可以使用注解@FunctionalInterface修饰 可以检查是否是函数式接口
@FunctionalInterface
public interface MyFun{
public Integer getValue(Integer num);
}
//需求:对一个数进行运算
@Test
public void test6(){
Integer num=operation(100,(x)->x*x);
System.out.println(num);
}
public Integer operation(Integer num,MyFun mf){
return mf.getValue(num);
}
3. java8中内置的四大核心函数式接口
- Consumer:消费性接口
-
void accept(T t);
- Supplier:供给型接口
-
T get();
- Function<T,R>:函数型接口
-
R apply(T t);
- Predicate:断言型接口
-
boolean test(T t);
//Consumer<T> 消费型接口
@Test
public void test1(){
happy(1000,(m)-> System.out.println("你好!"+m+"元"));
}
public void happy(double money, Consumer<Double> con){
con.accept(money);
}
// Supplier<T>:供给型接口
@Test
public void test2(){
List<Integer> numList=getNumList(10,()->(int)(Math.random()*100));
for(Integer num:numList){
System.out.println(num);
}
}
//需求:产生指定个数的正数,并放入集合中
public List<Integer> getNumList(int num, Supplier<Integer> sup){
List<Integer> list=new ArrayList<>();
for (int i = 0; i < num; i++) {
Integer n=sup.get();
list.add(n);
}
return list;
}
// Function<T,R>:函数型接口
@Test
public void test3(){
String newStr=strHandler("\t\t\t你好呀!",(str)->str.trim());
System.out.println(newStr);
}
//需求:用于处理字符串
public String strHandler(String str, Function<String,String>fun){
return fun.apply(str);
}
}
//Predicate<T>:断言型接口
@Test
public void test4(){
List<String> list= Arrays.asList("Hello","a","Hi","www");
List<String> strList=filterStr(list,(s)->s.length()>3);
for (String str:strList){
System.out.println(str);
}
}
//需求:将满足条件的字符串,放入集合中
public List<String> filterStr(List<String> list, Predicate<String> pre){
List<String> strList=new ArrayList<>();
for (String str:list){
if(pre.test(str)){
strList.add(str);
}
}
return strList;
}
更多推荐
所有评论(0)