JDK8-Lambda练习(二)

来源:互联网 发布:打造世界怎么修改数据 编辑:程序博客网 时间:2024/05/16 10:11
package cn.wcj.jdk8.lambda.exam;@FunctionalInterfacepublic 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 2017618日 上午10:29:34 */public class LambdaTest {    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, 61),                         new Emp("7777", "田七", 7777.77, 71)               )        );    @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(2, 4))  ;        System.out.println(subStr)  ;    }    public String handleString(String str,IStringFun stringFun) {           return stringFun.getVal(str)   ;    }    @Test    public void test3() {          System.out.println(operate(100L,200L,(x1,x2)->x1*x2));            System.out.println(operate(100L,200L,(x1,x2)->x1+x2));      }    public Long operate(Long x1,Long x2,ICountFunction<Long, Long> countFunction) {          return countFunction.count(x1, x2)  ;    }}
原创粉丝点击