自定义集合Map属性互转

BeanCopyUtils-自定义集合Map属性互转

背景

初次上班,写业务过程中发现项目里面没有common.lang3.BeanUtils类,只有spring自己引入的BeanUtils,只能copy单个对象,不能copy List或者Map,导致有些业务场景冗余代码过多,对后期维护不太友好, 限于公司不能随便加依赖以及改动项目整体架构,根据SpringFramework提供的Beanutils,就自己想到开发一个java bean copy工具类。

提示

我写的这个工具类是基于SpringFramework自带的BeanUtils写的。
第三方包都有提供这样的功能,如果各位小伙伴项目中有第三方BeanUtils工具类,可以直接用第三方包解决即可。

代码

/**
 * User实体类
 * @Author GaoJun
 * @Date Created in 14:34 2020/3/24
 */
public class User {
    private Integer id;
    private String name;
    private String userName;

    public Integer getId() {
        return id;
    }

    public User() {
    }

    public User(Integer id, String name, String userName) {
        this.id = id;
        this.name = name;
        this.userName = userName;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", userName='" + userName + '\'' +
                '}';
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
}

/**
 * User的Vo类
 * @Author GaoJun
 * @Date Created in 14:34 2020/3/24
 */
public class UserVo {
    private Integer id;
    private String name;
    private String nameVo;

    public UserVo(Integer id, String name, String nameVo) {
        this.id = id;
        this.name = name;
        this.nameVo = nameVo;
    }

    public UserVo() {
    }

    @Override
    public String toString() {
        return "UserVo{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", nameVo='" + nameVo + '\'' +
                '}';
    }

    public String getNameVo() {
        return nameVo;
    }

    public void setNameVo(String nameVo) {
        this.nameVo = nameVo;
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

}
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;

/**
 * java bean copy工具类
 * 作用于:集合内属性互转
 * @Author GaoJun
 * @Date 15:45 2020/3/24
 * <pre>{@code List互转:
 *       List<User> users = Arrays.asList(new User(1, "name1", "user1"), new User(2, "name2", "user2"), new User(3, "name3", "user3"));
 *       List<UserVo> userVoList = BeanCopyUtils.copyListProperties(users,UserVo::new,(user,userVO) -> {
 *             userVO.setNameVo(user.getUserName());
 *       });
 *       sout:
 *          UserVo{id=1, name='name1', nameVo='user1'}
 *          UserVo{id=2, name='name2', nameVo='user2'}
 *          UserVo{id=3, name='name3', nameVo='user3'}
 * }</pre>
 * <pre>{@code Map互转:
 *       List<User> users = Arrays.asList(new User(1, "name1", "user1"), new User(2, "name2", "user2"), new User(3, "name3", "user3"));
 *       Map<String,List<User>> map = new HashMap<>();
 *          map.put("1",users);
 *       List<User> users1 = Arrays.asList(new User(11, "name11", "user11"), new User(22, "name22", "user22"), new User(33, "name33", "user33"));
 *         map.put("2",users1);
 *       Map<String, List<UserVo>> map1 = Utils.copyMapProperties(map, UserVo::new, (user,userVo) -> {
 *           userVo.setNameVo(user.getUserName());
 *       });
 *       sout:
 *       1: UserVo{id=1, name='name1', nameVo='user1'}
 *          UserVo{id=2, name='name2', nameVo='user2'}
 *          UserVo{id=3, name='name3', nameVo='user3'}
 *       2: UserVo{id=11, name='name11', nameVo='user11'}
 *          UserVo{id=22, name='name22', nameVo='user22'}
 *          UserVo{id=33, name='name33', nameVo='user33'}
 * }</pre>
 */
public class BeanCopyUtils extends BeanUtils {
    /**
     * List数据的拷贝
     * @param sources: 数据源类
     * @param target:  目标类::new(eg: UserVO::new)
     * @return
     */
    public static <S, T> List<T> copyListProperties(List<S> sources, Supplier<T> target) {
        return copyListProperties(sources, target, null);
    }

    /**
     * Map数据的拷贝
     * @param sources: 数据源类
     * @param target:  目标类::new(eg: UserVO::new)
     * @return
     */
    public static  <K, S, T> Map<K, List<T>> copyMapProperties(Map<K, List<S>> sources, Supplier<T> target){
        return copyMapProperties(sources, target, null);
    }

    /**
     * 带回调函数的List数据的拷贝(可自定义字段拷贝规则)
     *
     * @param sources:  数据源类
     * @param target:   目标类::new(eg: UserVO::new)
     * @param callBack: 回调函数
     * @return
     */
    public static <S, T> List<T> copyListProperties(List<S> sources, Supplier<T> target, BeanCopyUtilCallBack<S, T> callBack) {
        List<T> list = new ArrayList<>(sources.size());
        for (S source : sources) {
            T t = target.get();
            copyProperties(source, t);
            list.add(t);
            if (callBack != null) {
                // 回调
                callBack.callBack(source, t);
            }
        }
        return list;
    }

    /**
     * 带回调函数的Map数据的拷贝(可自定义字段拷贝规则)
     * @param sources:  数据源类
     * @param target:   目标类::new(eg: UserVO::new)
     * @param callBack: 回调函数
     * @return
     */
    public static <K, S, T> Map<K, List<T>> copyMapProperties(Map<K, List<S>> sources, Supplier<T> target, BeanCopyUtilCallBack<S, T> callBack){
        Map<K, List<T>> map = new HashMap<>(sources.size());
        for (Map.Entry<K, List<S>> source : sources.entrySet()) {
            List<T> ts = copyListProperties(source.getValue(), target, callBack);
            map.put(source.getKey(),ts);
        }
        return map;
    }
}
@FunctionalInterface
interface BeanCopyUtilCallBack <S, T> {

    /**
     * 定义默认回调方法
     * @param t
     * @param s
     */
    void callBack(S t, T s);
}
import java.util.*;

/**
 * Main测试类
 * @Author GaoJun
 * @Date Created in 14:37 2020/3/24
 */
public class Test01 {
    public static void main(String[] args) {
        List<User> users = Arrays.asList(new User(1, "name1", "user1"), new User(2, "name2", "user2"), new User(3, "name3", "user3"));
        Map<String,List<User>> map = new HashMap<>();
        map.put("1",users);
        List<User> users2 = Arrays.asList(new User(11, "name11", "user11"), new User(22, "name22", "user22"), new User(33, "name33", "user33"));
        map.put("2",users2);
        Map<String, List<UserVo>> map1 = BeanCopyUtils.copyMapProperties(map, UserVo::new, (user,userVo) -> {
            userVo.setNameVo(user.getUserName());
        });
        map1.forEach((key,value) -> {
            System.out.print(key + ":");
            value.forEach(System.out::println);
        });
    }
}

在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值