list转map,list转tree,减少数据库查询次数

package com.oceano.utils;

import java.util.List;

public class TreeModel<K> {
private K node;
private List<TreeModel<K>> treeList;

public K getNode() {
return node;
}

public void setNode(K node) {
this.node = node;
}

public List<TreeModel<K>> getTreeList() {
return treeList;
}

public void setTreeList(List<TreeModel<K>> treeList) {
this.treeList = treeList;
}

@Override
public String toString() {
return "TreeModel [node=" + node + ", treeList=" + treeList + "]";
}

public TreeModel(K node, List<TreeModel<K>> treeList) {
super();
this.node = node;
this.treeList = treeList;
}

public TreeModel() {
super();
// TODO Auto-generated constructor stub
}

}




package com.oceano.utils;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;

public class ListToMapUtil {

@SuppressWarnings("unchecked")
public static <T, K> Map<K, List<T>> listToMap(String fieldName, List<T> list) {
if (null == fieldName) {
throw new IllegalArgumentException("fieldName is null");
}
if (null == list) {
throw new IllegalArgumentException("list is null");
}

Map<K, List<T>> map = new HashMap<>();

for (T t : list) {
K key = null;
try {
key = (K) PropertyUtils.getProperty(t, fieldName);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

if (null == key) {
throw new IllegalArgumentException("key is null");
}

if (null == map.get(key)) {
List<T> tList = new ArrayList<>();
tList.add(t);
map.put(key, tList);
} else {
List<T> tList = map.get(key);
tList.add(t);
}
}

return map;
}

}


package com.oceano.utils;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;

public class ListToTreeUtils {

public static <K, T> List<TreeModel<K>> listToTree(List<K> kList, T parentId, String parentFieldName,
String childFieldName) {

Map<T, List<K>> map = ListToMapUtil.listToMap(parentFieldName, kList);

List<TreeModel<K>> treeModelListReturn = new ArrayList<>();

List<K> listTemp = map.get(parentId);

treeModelListReturn = listToTreeFor(listTemp, childFieldName, map);

return treeModelListReturn;

}

@SuppressWarnings({ "rawtypes", "unchecked" })
private static <K, T> List<TreeModel<K>> listToTreeFor(List<K> kList, String childFieldName, Map<T, List<K>> map) {
List<TreeModel<K>> treeModelListReturn = new ArrayList<>();
for (K k : kList) {
TreeModel treeModel = new TreeModel();
treeModel.setNode(k);

String key;
try {
key = (String) PropertyUtils.getProperty(k, childFieldName);
List<K> listTemp2 = map.get(key);
if (listTemp2 != null) {
List<TreeModel<K>> childrenList = listToTreeFor(listTemp2, childFieldName, map);
treeModel.setTreeList(childrenList);
}
treeModelListReturn.add(treeModel);

} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
return treeModelListReturn;

}

}


package com.oceano.utils;

import java.util.ArrayList;
import java.util.List;

import com.oceano.modity.entity.T_Category;
import com.oceano.modity.vo.api.SieveConditionVo;

public class TreeModelToSieveConditionVoListUtils {

public static List<SieveConditionVo> treeModelToSieveConditionVoList(
List<TreeModel<T_Category>> categoryTreeModelList) {

List<SieveConditionVo> sieveConditionVoList = treeModelToSieveConditionVoListFor(categoryTreeModelList);

return sieveConditionVoList;

}

private static List<SieveConditionVo> treeModelToSieveConditionVoListFor(
List<TreeModel<T_Category>> categoryTreeModelList) {
List<SieveConditionVo> sieveConditionVoList = new ArrayList<>();

for (TreeModel<T_Category> treeModel : categoryTreeModelList) {
T_Category category = treeModel.getNode();

SieveConditionVo sieveConditionVo = new SieveConditionVo();
sieveConditionVo.setId(category.getId());
sieveConditionVo.setCode(category.getCateCode());
sieveConditionVo.setName(category.getCateName());
sieveConditionVo.setParentId(category.getParentId());

List<TreeModel<T_Category>> treeModelList = treeModel.getTreeList();
if (treeModelList != null && treeModelList.size() > 0) {
List<SieveConditionVo> childrenSieveConditionVoList = treeModelToSieveConditionVoListFor(treeModelList);
sieveConditionVo.setChildren(childrenSieveConditionVoList);
}
sieveConditionVoList.add(sieveConditionVo);
}

return sieveConditionVoList;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

jie310600

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值