扫二维码与项目经理沟通
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流
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);
}
我们在微信上24小时期待你的声音
解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流