Java8lambda新特性运用到项目中进行优化

现在java语言发展的越来越快了,java11下个月就要发布了,而我写下这文章的时候,才粗略看完<<java8实战书>>,将学到的java8新特性对自己之前编写的代码进行重构优化。因为不可能直接将我项目优化的代码过程展示出来,所以我写了个例子,简单易懂的描述出我代码优化的过程。第一次写博客,写的不好请多多包涵。

我这个需求是,通过一些字段查询记录,并返回需要的信息记录。
例如我有个用户类Record,里面有几个属性:

public class Record {
    private String name;
    private Integer age;
    private String sex;

    //省略Getter and Setter
}

而我要返回前端两个从Record拿到不同属性记录的需求,我分为老用户和新用户。

public class OldUserDTO{

    private String name;
    private Integer age;

    //省略Getter and Setter
}
public class NewUserDTO{

    private String sex;
    private String mobile;

    //省略Getter and Setter   
}

先模拟dao层查询数据库取出的记录集合

public static void main(String[] args) {
     //模拟dao层查询数据库取出的记录集合
     List<Record> list = dao();
}

public static List<Record> dao(){
        List<Record> list = new ArrayList<>();
        Record record = new Record();
        record.setName("张三");
        record.setAge(23);
        record.setSex("男");
        record.setMobile("15000000000");
        list.add(record);
        return list;
}

没学java8之前的写法

private static List<NewUserDTO> firstNewUserList(List<Record> list){
        List <NewUserDTO> newUserList = new ArrayList<>();
        for (Record record: list) {
            NewUserDTO dto = new NewUserDTO();
            dto.setMobile(record.getMobile());
            dto.setSex(record.getMobile());
            newUserList.add(dto);
        }
        return newUserList;
    }

    private static List<OldUserDTO> firstOldUserList(List<Record> list){
        List <OldUserDTO> oleUserList = new ArrayList<>();
        for (Record record: list) {
            OldUserDTO dto = new OldUserDTO();
            dto.setAge(record.getAge());
            dto.setName(record.getName());
            oleUserList.add(dto);
        }
        return oleUserList;
    }

学了java8之后的优化

private static List<NewUserDTO> twoNewUserList(List<Record> list){
      return list.stream().map(e -> new NewUserDTO().setSex(e.getSex()).setMobile(e.getMobile())).collect(Collectors.toList());
}

private static List<OldUserDTO> twoOldUserList(List<Record> list){
      return list.stream().map(e -> new OldUserDTO().setName(e.getName()).setAge(e.getAge())).collect(Collectors.toList());
}

上面写的代码如果属性多,会看起来很臃肿,所以我把它改成下面模式。现在Record类加两个静态转换DTO方法

public static NewUserDTO newUserConvert(Record record){
     NewUserDTO dto = new NewUserDTO();
     if (record != null) {
         dto.setMobile(record.getMobile());
         dto.setSex(record.getSex());
     }
     return dto;
 }

public static OldUserDTO oldUserConvert(Record record){
      OldUserDTO dto = new OldUserDTO();
      if (record != null) {
          dto.setAge(record.getAge());
          dto.setName(record.getName());
      }
      return dto;
}

然后就可以把例子2lambda表达式改成这样:

private static List<NewUserDTO> threeNewUserList(List<Record> list){
     return list.stream().map(Record::newUserConvert).collect(Collectors.toList());
}

private static List<OldUserDTO> threeOldUserList(List<Record> list){
     return list.stream().map(Record::oldUserConvert).collect(Collectors.toList());
}

大家有没有发现,上面的lambda除了map里面代码方法不一样,其他都一样,所以我做了最后的优化,先定义一个泛型方法

private static  <T,R> List<R> map(List<T> list, Function<T,R> f){
     return list.stream().map(f::apply).collect(Collectors.toList());   
}

然后想调用的时候,就直接调用这个通用方法就行了

map(list,Record::newUserConvert);
map(list,Record::oldUserConvert);

完整例子代码如下:

package lambdasinaction;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author chenshaohao
 * @date 2018/8/24
 */

public class Java8Demo {

    public static void main(String[] args) {
        //模拟dao层查询数据库取出的记录集合
        List<Record> list = dao();

        //没学java8之前的写法
        firstDemo(list);

        //学了java8之后的优化
        twoDemo(list);

        //进一步优化
        threeDemo(list);

        //最终优化
        fourDemo(list);
    }

    public static void firstDemo(List<Record> list){
        firstNewUserList(list);
        firstOldUserList(list);
    }

    public static void twoDemo(List<Record> list){
        twoNewUserList(list);
        twoOldUserList(list);
    }

    public static void threeDemo(List<Record> list){
        threeNewUserList(list);
        threeOldUserList(list);
    }

    public static void fourDemo(List<Record> list){
        map(list,Record::newUserConvert);
        map(list,Record::oldUserConvert);
    }

    private static List<NewUserDTO> firstNewUserList(List<Record> list){
        List <NewUserDTO> newUserList = new ArrayList<>();
        for (Record record: list) {
            NewUserDTO dto = new NewUserDTO();
            dto.setMobile(record.getMobile());
            dto.setSex(record.getMobile());
            newUserList.add(dto);
        }
        return newUserList;
    }

    private static List<OldUserDTO> firstOldUserList(List<Record> list){
        List <OldUserDTO> oleUserList = new ArrayList<>();
        for (Record record: list) {
            OldUserDTO dto = new OldUserDTO();
            dto.setAge(record.getAge());
            dto.setName(record.getName());
            oleUserList.add(dto);
        }
        return oleUserList;
    }

    private static List<NewUserDTO> twoNewUserList(List<Record> list){
        return list.stream().map(e -> new NewUserDTO().setSex(e.getSex()).setMobile(e.getMobile())).collect(Collectors.toList());
    }

    private static List<OldUserDTO> twoOldUserList(List<Record> list){
        return list.stream().map(e -> new OldUserDTO().setName(e.getName()).setAge(e.getAge())).collect(Collectors.toList());
    }

    private static List<NewUserDTO> threeNewUserList(List<Record> list){
        return list.stream().map(Record::newUserConvert).collect(Collectors.toList());
    }

    private static List<OldUserDTO> threeOldUserList(List<Record> list){
        return list.stream().map(Record::oldUserConvert).collect(Collectors.toList());
    }

    private static  <T,R> List<R> map(List<T> list, Function<T,R> f){
        List<R> result = list.stream().map(f::apply).collect(Collectors.toList());
        return result;
    }

    public static List<Record> dao(){
        List<Record> list = new ArrayList<>();
        Record record = new Record();
        record.setName("张三");
        record.setAge(23);
        record.setSex("男");
        record.setMobile("15000000000");
        list.add(record);
        return list;
    }

    public static class Record {
        private String name;
        private Integer age;
        private String sex;
        private String mobile;

        public static NewUserDTO newUserConvert(Record record){
            NewUserDTO dto = new NewUserDTO();
            if (record != null) {
                dto.setMobile(record.getMobile());
                dto.setSex(record.getSex());
            }
            return dto;
        }

        public static OldUserDTO oldUserConvert(Record record){
            OldUserDTO dto = new OldUserDTO();
            if (record != null) {
                dto.setAge(record.getAge());
                dto.setName(record.getName());
            }
            return dto;
        }

        public String getName() {
            return name;
        }

        public Record setName(String name) {
            this.name = name;
            return this;
        }

        public Integer getAge() {
            return age;
        }

        public Record setAge(Integer age) {
            this.age = age;
            return this;
        }

        public String getSex() {
            return sex;
        }

        public Record setSex(String sex) {
            this.sex = sex;
            return this;
        }

        public String getMobile() {
            return mobile;
        }

        public Record setMobile(String mobile) {
            this.mobile = mobile;
            return this;
        }
    }

    public static class OldUserDTO{

        private String name;
        private Integer age;

        public String getName() {
            return name;
        }

        public OldUserDTO setName(String name) {
            this.name = name;
            return this;
        }

        public Integer getAge() {
            return age;
        }

        public OldUserDTO setAge(Integer age) {
            this.age = age;
            return this;
        }
    }

    public static class NewUserDTO{

        private String sex;
        private String mobile;

        public String getSex() {
            return sex;
        }

        public NewUserDTO setSex(String sex) {
            this.sex = sex;
            return this;
        }

        public String getMobile() {
            return mobile;
        }

        public NewUserDTO setMobile(String mobile) {
            this.mobile = mobile;
            return this;
        }
    }
}
  • 3
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值