您好,登錄后才能下訂單哦!
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。