`
huangyongxing310
  • 浏览: 490451 次
  • 性别: Icon_minigender_1
  • 来自: 广州
文章分类
社区版块
存档分类
最新评论

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;

    }

}







分享到:
评论

相关推荐

    java版list-map实现 树结构 父子结构 通俗易懂

    此java类实现了对数据表的分类递归树的实现,为本人倾力之作,后期,会发布js版,敬请期待!

    Query对象setResultTransFormer()

    在上面的代码中,我们使用 setResultTransformer() 方法将查询结果转换为 Tree 对象,然后使用 list() 方法获取查询结果,最后遍历结果集,使用 Tree 对象来获取每个字段的值。 最后,让我们总结一下 ...

    【list转树结构】相关附件

    在IT行业中,数据结构是编程基础的重要组成部分,而树结构是一种非常常见且高效的数据结构,广泛应用于文件系统、数据库索引、编译器设计等多个领域。"List转树结构"通常指的是将一个扁平化的数据列表转换为树形结构...

    tree(c++ tree容器)

    C++标准库(STL)虽然提供了如vector、list、map等容器,但并没有直接提供树结构。标题提到的"tree(c++ tree容器)"是一个第三方实现,旨在为C++开发者提供一个类似于STL接口的树容器,方便用户在项目中构建和操作树...

    Algorithm-js-tree-list.zip

    "Algorithm-js-tree-list.zip"这个压缩包文件显然包含了关于如何将列表转换为树结构,并进行相关操作的代码资源。这种转换对于数据管理和操作,尤其是在处理层次关系时非常有用。下面我们将详细探讨相关的知识点。 ...

    对Map按key和value分别排序

    List&lt;Map.Entry, String&gt;&gt; list = new ArrayList(map.entrySet()); Collections.sort(list, new Comparator&lt;Map.Entry, String&gt;&gt;() { public int compare(Map.Entry, String&gt; obj1, Map.Entry, String&gt; obj2) { //...

    C语言版的STL,包含set,list,map等基本数据结构和算法.zip

    2. **List**: List是双向链表的实现,支持在任何位置快速插入和删除元素,时间复杂度为O(1)。在C语言中,可以使用结构体和指针来构建链表,并通过指针操作实现相应的功能。 3. **Map**: Map是关联容器,它将每个...

    java8 streamList转换使用详解

    在本文中,我们将深入探讨如何使用 Stream API 将 `List&lt;Map, Object&gt;&gt;` 转换为 `Map, Object&gt;`,以及如何统计字符串中字母的个数。 首先,让我们详细解析将 `List&lt;Map, Object&gt;&gt;` 转换为 `Map, Object&gt;` 的两种...

    数据结构Advanced-Data-Structures

    List 29 Stack 32 Queue 61 Deque 63 Priority queue 66 Map 70 Bidirectional map 73 Multimap 74 Set 75 Tree 80 Arrays 85 Array data structure 85 Row-major order 91 Dope vector 93 Iliffe vector 94 Dynamic...

    Java里多个Map的性能比较(TreeMap、HashMap、ConcurrentSkipListMap)

    在Java编程中,Map接口是用于存储键值对的数据结构,而Java提供了多种Map的实现,包括TreeMap、HashMap和ConcurrentSkipListMap。本文主要比较了这三种Map的性能,尤其是在插入和查找操作上的效率。 1. **TreeMap**...

    用Java集合递归实现通用树Tree

    Java集合框架是Java语言提供的一组接口和类,用于存储和操作各种数据结构,如列表(List)、队列(Queue)、集(Set)和映射(Map)。然而,标准的集合框架并没有直接提供对树结构的支持,因此我们需要自己创建。 ...

    B+ tree的java实现和C++实现

    B+树,全称B+ Tree,是一种自平衡的树数据结构,广泛应用于数据库和文件系统中,以高效地支持顺序访问、范围查询以及插入和删除操作。B+树的特点在于其所有数据都存储在叶子节点,且叶子节点之间通过指针链接,形成...

    java实现无限级分类树形,连接mysql数据库

    为了从MySQL数据库中获取无限级分类数据,我们可以使用递归查询或连接查询。这里我们演示递归查询的例子,使用MySQL的WITH RECURSIVE语句: ```sql WITH RECURSIVE tree AS ( SELECT * FROM tree_nodes WHERE ...

    ext树形动态菜单 .doc

    `MenuTreeUtil` 类负责将数据库查询的结果转换成树形结构,其核心方法 `importMenuTree` 如下所示: ```java public static List&lt;Map&gt; importMenuTree(List&lt;Map&gt; rs) { if (MatchUtil.isEmpty(rs)) { return null;...

    树tree、动态数组dyArray、hashMap、拼图算法.zip

    树的概念广泛应用于计算机科学中,如文件系统、数据库索引、编译器设计等。常见的树类型有二叉树、二叉搜索树、平衡树(AVL树、红黑树)等。理解树的遍历方法(前序、中序、后序)以及查找、插入和删除操作对于优化...

    JSP无限级分类目录树-sorttree.zip

    在JSP中,我们可以使用Java集合框架(如List、Map等)来存储和操作分类数据,然后利用HTML和CSS来构建可视化的树结构,同时可能还需要JavaScript进行交互处理,如点击展开/折叠节点。 在提供的“sortTree”文件中,...

    java框架代码

    在Spring中,`List`、`Map`和`Tree`等数据结构常用于配置 bean 容器、存储和处理数据。例如,`List` 可能用于定义一个bean集合,`Map` 可能用于动态配置属性,而`Tree`结构则可能出现在多层的配置或者目录结构中。 ...

    jQuery-Easyui 多级菜单 前后台

    List&lt;Tree&gt; list = new ArrayList&lt;Tree&gt;(); // 初始化菜单项 Tree tre1 = new Tree(); tre1.setId(1); tre1.setText("部门"); tre1.setState("closed"); tre1.setParent(0); list.add(tre1); // 其他菜单...

Global site tag (gtag.js) - Google Analytics