package com.hundsun.java.se.java8;
import org.junit.Test;
import java.util.Comparator;
import java.util.function.Consumer;
/**
* @Description: lambda表达式的基础语法
* @author:zhangys
* @date:Created in 16:33 2018/12/30
* @Modified By:
*/
public class DemoLamda {
/**
* 箭头操作符 的左侧对应接口方法中的参数,有几个就写几个
* 箭头操作符 的右侧对应接口方法中的函数体
*/
/**
* 语法格式一: 无参数 无返回值
* () -> System.out.println("hello word")
*/
@Test
public void test1(){
int num = 0; //jdk1.7之前,必须是final
Runnable r = new Runnable() {
@Override
public void run() {
System.out.println("hello");
}
};
r.run();
System.out.println("--------------");
Runnable r1 = () -> System.out.println("hello" + num);
r1.run();
}
/**
* 语法格式二:有一个参数,并且无返回值
*/
@Test
public void Test2(){
Consumer<String> con = (x) -> System.out.println(x);
con.accept("lambda语法格式");
}
/**
* 语法格式三:如果只有一个参数,小括号可以不写
*/
@Test
public void Test3(){
Consumer<String> con = x -> System.out.println(x);
con.accept("lambda语法格式");
}
/**
* 语法格式四 : 有两个以上的参数,并且lambda体中有多条语句,并且有返回值
*/
@Test
public void test4(){
Comparator<Integer> com = (x,y) -> {
System.out.println("函数式接口");
return Integer.compare(x,y);
};
}
/**
* 语法格式五:如果有两个参数,如果lambda体中只有一条语句,return和{} 都可以不写
*/
public void test5(){
Comparator<Integer> com = (x,y) -> Integer.compare(x,y);
}
/**
* lambda的数据类型可以不写,因为jvm的编译器可以通过上下文推断出数据类型,即“类型推断”
* 即 String[] str = {"123","333","444"};
*
* String[] str;
* str = {"111","222","333"}
*/
public void test6(){
Comparator<Integer> com = (Integer x,Integer y) -> Integer.compare(x,y);
}
/**
* 总结:左右遇一 括号省
* 左侧推断类型省 (数据类型省略)
* 能省则省
*/
/**
* 二、lambda表达式需要“函数式接口”的支持
* 函数式接口:如果接口中只有一个抽象方法,称为函数式接口,可以使用一个注解 @FunctionalInterface,可以检查接口是否是函数式接口
*/
}
四大内置核心函数式接口
package com.hundsun.java.se.java8;
import org.junit.Test;
import org.omg.PortableInterceptor.INACTIVE;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
/**
* @Description: 四大内置核心函数式接口
* @author:zhangys
* @date:Created in 19:05 2018/12/30
* @Modified By:
*/
public class DemoLambdaHanshu {
List<Employee> employees = Arrays.asList(
new Employee("zhangsan",19,9999.99),
new Employee("lisi",38,5555.55),
new Employee("wangwu",50,6666.66),
new Employee("zhaoliu",16,3333.33),
new Employee("tianqi",18,7777.77)
);
@Test
public void test(){
Collections.sort(employees,(e1,e2) -> {
if (e1.getAge() == e2.getAge()) {
return e1.getName().compareTo(e2.getName());
} else {
return Integer.compare(e1.getAge(),e2.getAge());
}
});
for (Employee e : employees){
System.out.println(e);
}
}
/**
* 四大核心函数式接口
* 1、consumer<T> 消费型接口
* void accept(T t)
* 2、Supplier<T> 供给型接口
* T get()
* 3、Function<T,R> 函数型接口
* R apply(T t)
* 4、Predicate<T> 断言型接口
* boolean test(T t)
*/
/**
* consumer
*/
@Test
public void test1(){
happy(10000,(m) -> System.out.println("消费" + m + "元"));
}
public void happy(double money, Consumer<Double> con){
con.accept(money);
}
/**
* supplier
*/
@Test
public void test2(){
List<Integer> list = getNumList(10,() -> (int)(Math.random() * 100));
for (Integer each : list){
System.out.println(each);
}
}
//产生一些整数,放入集合中
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
* @param str
* @param fun
* @return
*/
@Test
public void test3(){
String newStr = strHandler(" handler ",(str) -> str.trim());
System.out.println(newStr);
String subStr = strHandler("hhhhhhh",(str) -> str.substring(0,5));
System.out.println(subStr);
}
public String strHandler(String str, Function<String,String> fun){
return fun.apply(str);
}
/**
* Predicate
*/
@Test
public void test4(){
List<String> lsit = Arrays.asList("1111","222","44444","6666","asdfasd");
List<String> list = filterStr(lsit,(s) -> s.length() > 3);
for (String e : list){
System.out.println(e);
}
}
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;
}
}