您好,登錄后才能下訂單哦!
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);
}
//需求:對(duì)于兩個(gè)Long型數(shù)據(jù)進(jìn)行處理
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);
}
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。