java8 新特性-Lambda 表达式

最近在学习一些java8的新特性,觉得不错的就记载下来,以后查看 也方便,这边我直接以案例贴出来吧,好理解。

      以前我们做比较的时候一般是这样的:

     //原来的匿名内部类写法:

     @Test
     public void test1(){
         Comparator<String> com = new Comparator<String>(){
             @Override
             public int compare(String o1, String o2) {
                 return Integer.compare(o1.length(), o2.length());
             }
         };
         
         TreeSet<String> ts = new TreeSet<>(com);
         
         TreeSet<String> ts2 = new TreeSet<>( new Comparator<String>(){
             @Override
             public int compare(String o1, String o2) {
                 return Integer.compare(o1.length(), o2.length());
             }
             
         });
     }
 //现在的 Lambda 表达式:
  @Test
     public void test2(){
        Comparator<String> com =(x,y) ->Integer.campare(x.length(),y.length());
         TreeSet<String> ts = new TreeSet<>(com);
     }
下面我们来举一个工作中常遇到的问题:

   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, "赵六", 8, 7777.77),
            new Employee(105, "田七", 38, 5555.55)
    );
 
    //需求:获取公司中年龄小于 35 的员工信息
    public List<Employee> filterEmployeeAge(List<Employee> emps){
        List<Employee> list = new ArrayList<>();
         
        for (Employee emp : emps) {
            if(emp.getAge() <= 35){
                list.add(emp);
            }
        }
         
        return list;
    }
     
    @Test
    public void test3(){
        List<Employee> list = filterEmployeeAge(emps);
         
        for (Employee employee : list) {
            System.out.println(employee);
        }
    }
     
    //需求:获取公司中工资大于 5000 的员工信息
    public List<Employee> filterEmployeeSalary(List<Employee> emps){
        List<Employee> list = new ArrayList<>();
         
        for (Employee emp : emps) {
            if(emp.getSalary() >= 5000){
                list.add(emp);
            }
        }
         
        return list;
    }
//优化方式一:策略设计模式
     public List<Employee> filterEmployee(List<Employee> emps, MyPredicate<Employee> mp){
         List<Employee> list = new ArrayList<>();
         
         for (Employee employee : emps) {
             if (mp.test(employee)){
                 list.add(employee);
             }
         }
         
         return list;
     }
     @Test
     public void test4(){
         List<Employee> list = filterEmployee(emps, new FilterEmployeeForAge());
         for (Employee employee : list) {
             System.out.println(employee);
         }
         
         System.out.println( "------------------------------------------" );
         
         List<Employee> list2 = filterEmployee(emps, new FilterEmployeeForSalary());
         for (Employee employee : list2) {
             System.out.println(employee);
         }
     }

  //优化方式二:匿名内部类
     @Test
     public void test5(){
         List<Employee> list = filterEmployee(emps, new MyPredicate<Employee>() {
             @Override
             public boolean test(Employee t) {
                 return t.getId() <= 103 ;
             }
         });
         
         for (Employee employee : list) {
             System.out.println(employee);
         }
     }
  //优化方式三:Lambda 表达式
     @Test
     public void test6(){
         List<Employee> list = filterEmployee(emps, (e) -> e.getAge() <= 35 );
         list.forEach(System.out::println);
         
         System.out.println( "------------------------------------------" );
         
         List<Employee> list2 = filterEmployee(emps, (e) -> e.getSalary() >= 5000 );
         list2.forEach(System.out::println);
     }

 //优化方式四:Stream API

     @Test
     public void test7(){
         emps.stream()
             .filter((e) -> e.getAge() <= 35 )
             .forEach(System.out::println);
         
         System.out.println( "----------------------------------------------" );
         
         emps.stream()
             .map(Employee::getName)
             .limit( 3 )
             .sorted()
             .forEach(System.out::println);
     }

哈哈 最后优化的代码都快没了,越写越省事。。。。。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值