java-复制list<Object>

package com.cdsjty.pvb.modules.accident.utils;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cdsjty.pvb.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;

import java.util.*;

/**
 * 对两个list做copy工作
 * 对MybatisPlus里面的元素封装
 **/
@Slf4j
public class ListUtil<S,T> {
 

 
    public static <S,T> void copyIPage(IPage<S> siPage,IPage<T> tiPage,Class<T> tClass){
        if(siPage==null||tiPage==null){
            return;
        }
        tiPage.setCurrent(siPage.getCurrent());
        tiPage.setPages(siPage.getPages());
        tiPage.setSize(siPage.getSize());
        tiPage.setTotal(siPage.getTotal());
        List<S> sList= siPage.getRecords();
        List<T> tList=new ArrayList<>();
        copyList(sList,tList,tClass);
        tiPage.setRecords(tList);
    }
 
/**
 * 将list以某个符号分隔成字符串
 * @param list 字符串列表
 * @param symbol 符号
 * @return
 */
public static String getStringByList(List<String> list,String symbol){
    if(StringUtils.isEmpty(list)){
        return "";
    }
    StringBuilder stringBuilder=new StringBuilder();
    for(int i=0;i<list.size();i++){
        if(i!=list.size()-1){
            stringBuilder.append(list.get(i)).append(symbol);
        }else{
            stringBuilder.append(list.get(i));
        }
    }
    return stringBuilder.toString();
}
 
/**
 * 将set以某个符号分隔成字符串
 * @param set set
 * @param symbol 符号
 * @return
 */
public static String getStringBySet(Set<String> set, String symbol){
    List<String> stringList=new ArrayList<>(set);
    return getStringByList(stringList,symbol);
}
 
public static List<String> getListByString(String s){
    List<String> list=new ArrayList<>();
    if(StringUtils.isEmpty(s)){
        return list;
    }
    String[] array=s.split(",");
    list= Arrays.asList(array);
    return list;
}
 
public static List<String> getListByString(String s,String symbol){
    List<String> list=new ArrayList<>();
    if(StringUtils.isEmpty(s)){
        return list;
    }
    String[] array=s.split(symbol);
    list=Arrays.asList(array);
    return list;
}
 
public static <S,T> void copyList(Collection<S> sCollection, Collection<T> tCollection, Class<T> tClass) {
    if(sCollection==null||tCollection==null||sCollection.size()==0){
        return;
    }
    try {
        for(S s:sCollection){
            T t = tClass.newInstance();
            BeanUtils.copyProperties(s,t);
            tCollection.add(t);
        }
    } catch (Exception e) {
        log.error("列表转换失败",e);
    }
}
 
public static <S,T> List<T> copyList(Collection<S> sCollection,Class<T> tClass) {
    List<T> list=new ArrayList<>();
    if(sCollection==null||sCollection.size()==0){
        return list;
    }
    try {
        for(S s:sCollection){
            T t = tClass.newInstance();
            BeanUtils.copyProperties(s,t);
            list.add(t);
        }
    } catch (Exception e) {
        log.error("列表转换失败",e);
    }
    return list;
}
 
 
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值