您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
List<Employee> 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<Employee> 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<T, R> {
public R getValue(T t1, T t2);
}
//需求:对于两个Long型数据进行处理
public void op(Long l1, Long l2, MyFunction2<Long, Long> 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);
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。