天天看點

JDK8-Lambda練習(二)

package cn.wcj.jdk8.lambda.exam;

@FunctionalInterface
public interface ICountFunction<P,R> {

    R count(P p1,P p2);     

}
           
package cn.wcj.jdk8.lambda.exam;

public interface IStringFun {

    String getVal(String val)   ;

}
           
package cn.wcj.jdk8.lambda.exam;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.junit.Test;

import cn.wcj.jdk8.lambda.test.Emp;
/**
 * 
* <p>Title:LambdaTest </p>
* <p>Description: Lambda表達式練習</p>
* <p>Company:Software College </p> 
* @author SuccessKey(WangCJ)
* @date 年月日 上午::
 */
public class LambdaTest {

    List<Emp> emps=new ArrayList<Emp>(
            Arrays.asList(
               new Emp("3333", "張三", , ),          
               new Emp("4444", "李四", , ),          
               new Emp("5555", "王五", , ),          
               new Emp("6666", "趙六", , ),          
               new Emp("7777", "田七", , )   
            )
        );

    @Test
    public void test1() {
          Collections.sort(emps, (e1,e2)->{
              if(e1.getAge()==e2.getAge()) 
                   return e1.getEname().compareTo(e2.getEname())   ;
              else
                   return e1.getAge().compareTo(e2.getAge())   ;
          });
          emps.stream().forEachOrdered(System.out::println);
    }


    @Test
    public void test2() {
        String trim=handleString("\t\t\t\t\t\t  我大軟院威武!!!  ",(str)->str.trim())  ;
        System.out.println(trim)   ;
        String upper=handleString("abcdefg", (str)->str.toUpperCase())   ;
        System.out.println(upper)   ;
        String subStr=handleString("我大軟院威武", (str)->str.substring(, ))  ;
        System.out.println(subStr)  ;
    }

    public String handleString(String str,IStringFun stringFun) {
           return stringFun.getVal(str)   ;
    }


    @Test
    public void test3() {
          System.out.println(operate(L,L,(x1,x2)->x1*x2));  
          System.out.println(operate(L,L,(x1,x2)->x1+x2));  
    }

    public Long operate(Long x1,Long x2,ICountFunction<Long, Long> countFunction) {
          return countFunction.count(x1, x2)  ;
    }







}