jdk 8 中 Lambda 表达式练习题(经典面试题)

jdk 8 中 Lambda 表达式练习题(经典面试题)

题1

调用 Collection.sort()方法,通过定制排序比较两个Employee(先按年龄,年龄相同按姓名比)。

题2

①声明函数式接口,接口中声明抽象方法,public String getValue();
②声明测试类,在类中编写方法使用接口作为参数,将每一个字符品转换成大写,并作为方法的返回值
③再将一个字符串的第2个和第4个索引位置进行截取子串

题3

①声明一个带两个泛型的函数式接口,泛型类型为<T,R> T为参数,R为返回值
②接口中声明对应抽象方法
③在测试类中声明方法,使得接口作为参数,计算两个long型参数的和
④再计算两个long型参数的乘积

  • pom.xml
<dependencies>
       <dependency>
           <groupId>junit</groupId>
           <artifactId>junit</artifactId>
           <version>4.12</version>
           <scope>compile</scope>
       </dependency>
       <dependency>
           <groupId>org.projectlombok</groupId>
           <artifactId>lombok</artifactId>
           <version>1.18.10</version>
       </dependency>
   </dependencies>

   <properties>
       <maven.compiler.source>8</maven.compiler.source>
       <maven.compiler.target>8</maven.compiler.target>
   </properties>
  • 核心代码
public class TestLambda {

   List<Employee> emps = Arrays.asList(
           new Employee(101, "张三", 18, 9999.99),
           new Employee(102, "李四", 59, 6666.66),
           new Employee(103, "王五", 28, 3333.33),
           new Employee(104, "赵六", 18, 7777.77),
           new Employee(105, "田七", 38, 5555.55)
   );

   /**
    * 要求:调用 Collection.sort()方法,通过定制排序比较两个Employee(先按年龄,年龄相同按姓名比)。
    */
   @Test
   public void test1() {

       Collections.sort(emps,
               (x, y) -> {
                   if (x.getAge() == y.getAge()) {
                       return Double.compare(x.getSalary(), y.getSalary());
                   } else {
                       return Integer.compare(x.getAge(), y.getAge());
                   }
               }
       );
       System.out.println("emps = " + emps);
   }

   /**
    * 要求:
    * ①声明函数式接口,接口中声明抽象方法,public String getValue();
    * ②声明测试类,在类中编写方法使用接口作为参数,将每一个字符品转换成大写,并作为方法的返回值
    * ③再将一个字符串的第2个和第4个索引位置进行截取子串
    */
   @Test
   public void test2() {
       String str = "admin NBA Cba";
       String valueF = getValueF(str,
               x -> x.substring(0, 1).toUpperCase() + x.substring(1)

       );
       System.out.println("valueF = " + valueF);

       String valueF2 = getValueF(str,
               x -> x.substring(1, 3)
       );
       System.out.println("valueF2 = " + valueF2);

   }

   public String getValueF(String str, MyFunction1 string2UpChar) {
       return string2UpChar.getValue(str);
   }

   /**
    * 要求:
    * ①声明一个带两个泛型的函数式接口,泛型类型为<T,R> T为参数,R为返回值
    * ②接口中声明对应抽象方法
    * ③在测试类中声明方法,使得接口作为参数,计算两个long型参数的和
    * ④再计算两个long型参数的乘积
    */
   @Test
   public void test3() {

       String calc = calc(10L, 3L,
               (a, b) -> new BigDecimal(a).divide(new BigDecimal(b), 3, BigDecimal.ROUND_HALF_UP).toString()
       );
       System.out.println("calc = " + calc);

   }

   public String calc(Long l1, long l2, MyFunction2<Long, String> myFunction2) {

       return myFunction2.calc(l1, l2);
   }

}

@Data
@Builder
@Accessors(chain = true)
@NoArgsConstructor
@AllArgsConstructor
class Employee {

   private int id;
   private String name;
   private int age;
   private double salary;

}

@FunctionalInterface
interface MyFunction1 {

   public String getValue(String str);

}

@FunctionalInterface
interface MyFunction2<T, R> {

   public R calc(T t1, T t2);

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值