分割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;
}
}