java常用方法笔记

分割list

/**
* 将源List按照指定元素数量拆分为多个List
*
* @param source 源List
* @param splitItemNum 每个List中元素数量
*/
    public static <T> List<List<T>> averageAssign(List<T> source, int splitItemNum) {
        List<List<T>> result = new ArrayList<>();

        if (source != null && source.size() > 0 && splitItemNum > 0) {
            if (source.size() <= splitItemNum) {
                // 源List元素数量小于等于目标分组数量
                result.add(source);
            } else {
                // 计算拆分后list数量
                int splitNum = (source.size() % splitItemNum == 0) ? (source.size() / splitItemNum) : (source.size() / splitItemNum + 1);

                List<T> value = null;
                for (int i = 0; i < splitNum; i++) {
                    if (i < splitNum - 1) {
                        value = source.subList(i * splitItemNum, (i + 1) * splitItemNum);
                    } else {
                        // 最后一组
                        value = source.subList(i * splitItemNum, source.size());
                    }
                    result.add(value);
                }
            }
        }
        return result;
    }

List按其他list排序

public static void setListOrder(List<String> orderRegulation, List<List<NewPayoutChargeList>> targetList) {
       // 按照 list 里的 getContractNbr来排序 targetList
       Collections.sort(targetList, ((o1, o2) -> {
           int io1 = orderRegulation.indexOf(o1.get(0).getContractNbr());
           int io2 = orderRegulation.indexOf(o2.get(0).getContractNbr());

           if (io1 != -1) {
               io1 = targetList.size() - io1;
           }
           if (io2 != -1) {
               io2 = targetList.size() - io2;
           }

           return io2 - io1;
       }));
   }

List实现类似sql的group by功能


// 按里面两个属性分组,相同的会被分为一组,不同的一组。
 Map<String, Map<String, List<NewPayoutChargeList>>> collectMap = bodies.stream().collect(Collectors.groupingBy(NewPayoutChargeList::getContractNbr,
                            Collectors.groupingBy(NewPayoutChargeList::getContractName)));
// 取出举例
 List<List<NewPayoutChargeList>> listsPs = new ArrayList<>();
 for (Map.Entry<String, Map<String, List<NewPayoutChargeList>>> entryMap : collectMap.entrySet()) {
     Map<String, List<NewPayoutChargeList>> stringListMap = collectMap.get(entryMap.getKey());
     for (Map.Entry<String, List<NewPayoutChargeList>> map : stringListMap.entrySet()) {
         List<NewPayoutChargeList> newPayoutChargeLists = JSONArray.parseArray(JSONArray.toJSONString(map.getValue()), NewPayoutChargeList.class);
         listsPs.add(newPayoutChargeLists);
     }
 }

数据格式

拷贝List
@FunctionalInterface
public interface  CopyBeanUtilsCallBack<S, T> {
    void callBack(S t, T s);
}

package com.tianyuan.customer.core.util;

import com.tianyuan.common.core.util.bean.BeanUtils;

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

/**
 * @projectName: ms
 * @package: com.tianyuan.customer.core.util
 * @className: BeanUtil
 * @author: tang wen jun
 * @description: copyBean
 * @date: 2022年10月09日 19:22
 */
public class BeanUtil extends BeanUtils {

    /**
     * @param sources  数据源类
     * @param target   目标类
     * @param callBack [sources, target, callBack] 回调,可以在里面格式化返回值List
     * @return java.util.List<T>
     * @description: 使用场景:Entity、Bo、Vo层数据的复制,因为BeanUtils.copyProperties只能给目标对象的属性赋值,却不能在List集合下循环赋值,因此添加该方法
     * 如:List<AccountEntity> 赋值到 List<AccountVo>中的 AccountVo 属性都会被赋予到值
     * {@link com.tianyuan.ms.customer.service.impl.ContactsInfoServiceImpl#findByAccountId}
     * @author tang wen jun
     * @date 2022/10/9 15:04
     */
    public static <S, T> List<T> copyListProperties(List<S> sources, Supplier<T> target, CopyBeanUtilsCallBack<S, T> callBack) {
        List<T> list = new ArrayList<>(sources.size());
        for (S source : sources) {
            T t = target.get();
            copyProperties(source, t);
            if (callBack != null) {
                // 回调
                callBack.callBack(source, t);
            }
            list.add(t);
        }
        return list;
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值