package model;
import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.IntFunction;
import java.util.function.Supplier;
/**
* java8 的lambda 学习案例
*
* @author ZLL
* @version 1.0
* @since 2020年2月23日17:05:05
*/
public class Java8LambdaTest {
public static void main(String[] args) {
// test001();
// test002();
test006();
}
/**
* 1,为什么要学习lambda
* <p>函数式编程风格的兴起</p>
* <p>java存在很多做是用一次的匿名内部函数写法,嵌套太深,影响代码可读性</p>
* <p>lambda 提供了基于语义化 的api ,可读性高</p>
* 2,lambda 缺点
* <p>表达式不好断点调试,内部的执行状态被封装了,只能打印一些日志调试</p>
*
* @see java.lang.Comparable
* @see java.util.Comparator
* @see Runnable //java 默认导入了lang包
*/
public static void test001() {
List<Integer> list = Lists.newArrayList(1, 12, 3, 100, 4, 7, 1000);
//使用comparator 排序
/**
* @see java.util.Collections#sort(List, Comparator) ;
*/
Collections.sort(list, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1.intValue() - o2.intValue();//ASC
}
});
System.out.println(JSON.toJSONString(list));
}
/**
* 使用lambda的排序
*/
public static void test002() {
List<Integer> list = Lists.newArrayList(1, 12, 3, 100, 4, 7, 1000);
// Collections.sort(list,(a1, a2)->a1-a2);
/**
* @see Comparator#comparing(Function)
*/
Collections.sort(list, Comparator.comparingInt(a -> a));
System.out.println(JSON.toJSONString(list));
}
/**
* 相关的认识
*
* @FunctionalInterface 这个是编译器级别的一个注解,类似Override,
* 只有一个方法未实现
* @see Runnable
*/
public void test003() {
Thread thread = new Thread(() -> System.out.println("this is lambda thread run..."));
thread.start();
/**
* Comparator 的常用方法
* @see Comparator#comparing(Function)
* 二次排序
* @see Comparator#thenComparing(Comparator)
* @see Comparator#reversed()
* @see Comparator#naturalOrder()
*
* <p>
* (int a, int b)->{Systemc.out.println(a-b)};
* lambda参数,箭头,lambda主体
* </p>
*/
}
/**
* 认识5种函数式的接口
*
* @see java.util.function.Predicate 接收一个参数,返回一个bool 值 boolean test(T t)
* @see java.util.function.Consumer 接收一个参数,没有返回值 void accept(T t);
* @see java.util.function.Function 接收一个参数,返回另一个参数 R apply(T t);
* @see java.util.function.Supplier 无参数,返回一个参数,T get();
* @see java.util.function.BinaryOperator R apply(T t, U u);
* -> 也是一个function ,接收两个参数,返回一个参数
*/
public static void test004() {
//java8 的类型推断,与函数签名,编译期间的检查 ,
// 这里分两种类型的,原始的,和对象的,避免拆箱引起的过多性能损耗
/**
* T->boolean
* 原始的
* @see java.util.function.LongPredicate
* @see java.util.function.IntPredicate
* @see java.util.function.DoublePredicate
*
* T->void
* @see java.util.function.IntConsumer
* @see java.util.function.LongConsumer
* @see java.util.function.DoubleConsumer
*
* T->R
* @see java.util.function.IntFunction
* @see java.util.function.LongFunction
* @see java.util.function.DoubleFunction
*
*
* void->T
* @see java.util.function.BooleanSupplier
* @see java.util.function.IntSupplier
* @see java.util.function.LongSupplier
* @see java.util.function.DoubleSupplier
*
*
* {@link java.util.function.BiFunction}
* (T,U)->R
* @see java.util.function.ToIntBiFunction
* @see java.util.function.ToLongBiFunction
* @see java.util.function.ToDoubleBiFunction
*
*
* //接收两个参数的(T,U)->void
* {@link java.util.function.BiConsumer}
* @see java.util.function.ObjIntConsumer
* @see java.util.function.ObjLongConsumer
* @see java.util.function.ObjDoubleConsumer
*
*
* (T, U)->boolean
* {@link java.util.function.BiPredicate}
*
*
*
* T->T
* {@link java.util.function.UnaryOperator}
* @see java.util.function.Function
* (T,T)->T
* {@link java.util.function.BinaryOperator}
* @see java.util.function.BiFunction
*/
}
/**
* 方法引用,更进一步的简化书写,消除掉了lambda 的语法糖
*/
public static void test005() {
// Thread.currentThread().dumpStack();
// Thread.currentThread()::dumpStack;
//公式
/**
* lambda (args)->ClassName.staticMethod(args);
* 等价方法引用
* ClassName::staticMethod
*
* (String s)->System.out.println(s)===>等价System.out::println
*/
/**
* lambda(arg0, arg1)->arg0.instantMethod(arg1)
* 等价方法引用
* arg0的ClassName::instantMethod
* (String str, int i)->str.subString(i)===>String::subString
*
* (Apple a)->a.getWeight() ===>等价 Apple::getWeight
*/
/**
* (args)->express.instantMethod(args);
*
* ()->Thread.currentThread().dumpStack()===>Thread.currentThread::dumpStack
*
*/
}
/**
* 构造函数的方法引用
*/
public static void test006(){
//无参数的
Supplier<Apple> supplier = Apple::new;
Apple apple = supplier.get();
System.out.println(apple);
//有参数的
/**
* @see java.util.function.IntFunction
*/
Function<Integer,Apple> function = (weight)->new Apple(weight);
Apple apple2 = function.apply(10);
System.out.println(apple2);
//等价于
Function<Integer,Apple> function2 = Apple::new;
//指向apple的new Apple(weight) 构造方法
System.out.println(function2.apply(30));
//等价于,避免拆箱操作
IntFunction<Apple> intFunction = Apple::new;
System.out.println(intFunction.apply(1000));
}
}