大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
List employees = Arrays.asList(
new Employee("张三", 18 ,9999.99),
new Employee("李四", 50, 5555.99),
new Employee("王五", 50, 6666.66),
new Employee("赵六", 16, 3333.33),
new Employee("田七", 8, 7777.77)
);
@Test
public void test1(){
Collections.sort(employees, (e1, e2) -> {
if (e1.getAge() == e2.getAge()){
return e1.getName().compareTo(e2.getName());
}else {
return Integer.compare(e1.getAge(), e2.getAge());
//倒序排
//return -Integer.compare(e1.getAge(), e2.getAge());
}
});
for (Employee employee : employees){
System.out.println(employee);
}
}
@FunctionalInterface
public interface MyFunction {
public String getValue(String str);
}
List employees = Arrays.asList(
new Employee("张三", 18 ,9999.99),
new Employee("李四", 50, 5555.99),
new Employee("王五", 50, 6666.66),
new Employee("赵六", 16, 3333.33),
new Employee("田七", 8, 7777.77)
);
//需求:用于处理字符串
public String strHandler(String str, MyFunction mf){
return mf.getValue(str);
}
@Test
public void test2(){
String trimStr = strHandler("abfdfdf", (str) -> str.toUpperCase());
System.out.println(trimStr);
String subStr = strHandler("abfdfdf", (str) -> str.substring(2, 4));
System.out.println(subStr);
}
public interface MyFunction2 {
public R getValue(T t1, T t2);
}
//需求:对于两个Long型数据进行处理
public void op(Long l1, Long l2, MyFunction2 mf){
System.out.println(mf.getValue(l1, l2));
}
@Test
public void test3(){
op(100L, 200L, (x, y) -> x + y);
op(100L, 200L, (x, y) -> x * y);
}