package cn.wcj.jdk8.lambda.exam;



import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Function;

import org.junit.Test;

import cn.wcj.jdk8.lambda.test.Emp;
/**
 * 
* <p>Title:LambdaTest2 </p>
* <p>Description:使用方法引用、构造引用、数组引用改造之前练习题 </p>
* <p>Company:Software College </p> 
* @author SuccessKey(WangCJ)
* @date 2017年6月19日 下午5:33:39
 */
public class LambdaTest2 {

    List<Emp> emps=new ArrayList<Emp>(
            Arrays.asList(
               new Emp("3333", "张三", 3333.33, 31),          
               new Emp("4444", "李四", 4444.44, 41),          
               new Emp("5555", "王五", 5555.55, 51),          
               new Emp("6666", "赵六", 6666.66, 71),          
               new Emp("7777", "田七", 7777.77, 71)   
            )
        );

    @Test
    public void test1() {
       Function<String, String> strFun=String::trim  ;
       System.out.println(strFun.apply("\t\t\t\t\t\t  我大软院威武!!!  "))  ;
       Function<String, String> strFun2=String::toLowerCase    ;
       System.out.println(strFun2.apply("abcdefg"))   ;
       Function<String,String> strFun3=str->str.substring(2, 4)   ;
       System.out.println(strFun3.apply("我大软院威武"));
    }

    @Test
    public void test2(){
        //大写:ctrl+shift+x
        //小写:ctrl+shift+y
        BiFunction<Long, Long, Long> bf=(x,y)->x*y     ;
        System.out.println(bf.apply(100L,200L));
        BiFunction<Long, Long, Long> bf2=(x,y)->x+y    ;
        System.out.println(bf2.apply(100L, 200L));
    }

    //代码变得复杂了,只要是为了改造之前的代码,有点大材小用
    @Test
    public void test3(){
        Collections.sort(emps, (e1,e2)->{
            BiFunction<Integer, Integer, Integer> ageCp=Integer::compareTo   ;
            BiFunction<String, String, Integer> enameCp=String::compareTo    ;
              if(e1.getAge()==e2.getAge()) 
                   return enameCp.apply(e1.getEname(), e2.getEname())  ;
              else
                   return ageCp.apply(e1.getAge(), e2.getAge())        ;
        })   ;
        emps.stream().forEach(System.out::println)   ;
    }






}