Java8新特性_Lambda 练习
1、自定义Comparator来进行多级排序 Collections.sort()。
2、自定义函数式接口进行字符串处理。
- 1、根据需求写自定义方法func1(),在该方法中调用函数式接口的方法进行处理。
- 2、声明并编写函数式接口MyFunction。
- 3、调用func1()方法,并将自定义的Lambda表达式视作可以传递的代码传入。
从上面的函数是接口MyFunction可以窥见对Java8中内置的特定函数是接口的选择是熟练使用Lambda表达式的重中之重
主测试类:
package com.atguigu.exer;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.Test;
import com.atguigu.java8.Employee;
public class TestLambda {
List<Employee> emps = Arrays.asList(
new Employee(101, "张三", 18, 9999.99),
new Employee(102, "李四", 59, 6666.66),
new Employee(103, "王五", 28, 3333.33),
new Employee(104, "赵六", 8, 7777.77),
new Employee(105, "田七", 38, 5555.55)
);
//自定义Comparator来进行多级排序 Collections.sort()
@Test
public void test1(){
Collections.sort(emps, (e1, e2) -> {
if(e1.getAge() == e2.getAge()){
return e1.getName().compareTo(e2.getName());
}else{
return -Integer.compare(e1.getAge(), e2.getAge());
}
});
for (Employee emp : emps) {
System.out.println(emp);
}
}
@Test
public void test2(){
String trimStr = strHandler("\t\t\t 我大尚硅谷威武 ", (str) -> str.trim());
System.out.println(trimStr);
String upper = strHandler("abcdef", (str) -> str.toUpperCase());
System.out.println(upper);
String newStr = strHandler("我大尚硅谷威武", (str) -> str.substring(2, 5));
System.out.println(newStr);
}
//需求:用于处理字符串
public String strHandler(String str, MyFunction mf){
return mf.getValue(str);
}
@Test
public void test3(){
op(100L, 200L, (x, y) -> x + y);
op(100L, 200L, (x, y) -> x * y);
}
//需求:对于两个 Long 型数据进行处理
public void op(Long l1, Long l2, MyFunction2<Long, Long> mf){
System.out.println(mf.getValue(l1, l2));
}
}
自定义函数式接口
package com.atguigu.exer;
//函数式接口可以作如此声明。它将检查是否符合函数是接口的规则
@FunctionalInterface
public interface MyFunction {
String getValue(String str);
}
-------------------------------------------------
package com.atguigu.exer;
public interface MyFunction2<T, R> {
R getValue(T t1, T t2);
}

浙公网安备 33010602011771号