递归处理List集合【无限级分类的实现】

class Location {  
    private int id; //id  
    private String name; //名称  
    private int parentId; //父id  
    //为了组合多维集合添加的属性  
    private List<Location> list;
	//GetSet...
}  
public class T2 {
	
	public static void main(String[] args) {
		//一维
		List<Location> list = getChilds(data, 1);
		for(Location l : list){
			System.out.println(l.getName());
		}
		System.out.println("===============");
		//多维
		List<Location> list2 = getChildsManyGroup(data, 1);
		for(Location l : list2){
			System.out.println(l.getName());
			for(Location ll : l.getList()){
				System.out.println("\t"+ll.getName());
				for(Location lll : ll.getList()){
					System.out.println("\t\t"+lll.getName());
					for(Location lllL : lll.getList()){
						System.out.println("\t\t\t"+lllL.getName());
						
					}
				}
			}
		}
	}
	//查询数据库表中所有数据,(这里模拟一个表的所有数据)
	static List<Location> data = new ArrayList<Location>();  
    static{  
        //new Location(编号, "名称", 父编号);  
        Location l = new Location(1, "编程语言", 0);  
        Location l1 = new Location(2, "数据库", 0);  
        Location l2 = new Location(3, "Java", 1);  
        Location l3 = new Location(4, ".NET", 1);  
        Location l4 = new Location(5, "java EE", 3);  
        Location l5 = new Location(6, "java SE", 3);  
        Location l6 = new Location(7, "java ME", 3);  
        Location l7 = new Location(8, "asp.NET", 4);  
        Location l8 = new Location(9, "ado.NET", 4);  
        Location l9 = new Location(10, "MySQL", 2);  
        Location l10 = new Location(11, "Oracle", 2);  
        Location l11 = new Location(12, "hibernate", 5);  
        Location l12 = new Location(13, "hibernate 3.5", 12); 
        Location l13 = new Location(14, "hibernate 4.2", 12);
        data.add(l);data.add(l1);data.add(l2);  
        data.add(l3);data.add(l9);data.add(l10);  
        data.add(l4);data.add(l5);data.add(l6);data.add(l7);data.add(l8);data.add(l11);
        data.add(l12);data.add(l13);
    }  
    /** 
     * 根据id 获取所有父级目录 一维 
     * 用途(首页 > 编程语言 > JAVA >  hibernate > hibernate入门教程)类似这样的一个系统当前位置   传入位置获取到所有父级 
     * @param list 
     * @param pid 
     * @return 
     */  
    public static List<Location> getParents(List<Location> list,int pid){  
        List<Location> arr = new ArrayList<Location>();  
        for (Location location : list) {  
            if(pid == location.getId()){  
                arr.addAll(getParents(list, location.getParentId()));  
                arr.add(location);  break;
            }  
        }  
        return arr;  
    }  
    /**
     * 根据id获取所有子集分类(一维List集合)
     * 1 11 111
     * @param list
     * @param pid
     * @return
     */
    public static List<Location> getChilds(List<Location> list,int pid){
    	List<Location> arr = new ArrayList<Location>();
    	for(Location location : list){
    		if(pid == location.getParentId()){
    			arr.addAll(getChilds(list, location.getId()));
    			arr.add(location);
    			
    		}
    	}
    	return arr;
    }
    /**
     * 根据id返回所有子集分类,(多维List集合,List中含有子集List)
     * 
     *  1
     *    11
     *       111
     *  2
     *    22
     *       222 
     * @param list
     * @param pid
     * @return
     */
    public static List<Location> getChildsManyGroup(List<Location> list,int pid){
    	List<Location> arr = new ArrayList<Location>();
    	for(Location location : list){
    		if(pid == location.getParentId()){
    			location.setList(getChildsManyGroup(list, location.getId()));  
                arr.add(location); 
    		}
    	}
    	return arr;
    }
      
    /** 
     * 组合为一维集合 
     * @param list 
     * @param pid 
     * @return 
     */  
    public static List<Location> pushOneGroup(List<Location> list,int pid){  
        List<Location> arr = new ArrayList<Location>();  
        for (Location location : list) {  
            if(pid == location.getParentId()){  
                arr.add(location);  
                arr.addAll(pushOneGroup(list, location.getId()));  
            }  
        }  
        return arr;  
    }  
    /** 
     * 组合为多维集合 
     * 用途:系统首页的导航菜单,常见的2-3级通过下面的方法压栈成多维集合在供前台显示 
     * @param list 要便利的集合数据 
     * @param pid  父id 
     * @return 
     */  
    public static List<Location> pushManyGroup(List<Location> list,int pid){  
        List<Location> arr = new ArrayList<Location>();  
        for (Location location : list) {  
            if(pid == location.getParentId()){  
                location.setList(pushManyGroup(list, location.getId()));  
                arr.add(location);  
            }  
        }  
        return arr;  
    }  
      
}  


  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
实现无限分类的方法有多种,其中包括归和非归两种方式。下面分别介绍一下这两种方法的实现。 1. 实现无限分类 实现无限分类的方法是将分类数据作为一个树形结构来处理。具体实现步骤如下: (1)从数据库中获取所有分类数据,并按照父子关系组织为树形结构。 (2)遍历树形结构,输出每个分类的名称以及子分类的名称。 (3)对于每个子分类,重复步骤(2)。 下面是一个实现无限分类的示例代码: ```php function getCategoryTree($parent_id = 0, $level = 0) { $categories = getCategoryByParentId($parent_id); if (count($categories) > 0) { foreach ($categories as $category) { echo str_repeat('-', $level) . $category['name'] . '<br>'; getCategoryTree($category['id'], $level + 1); } } } function getCategoryByParentId($parent_id) { // 从数据库中获取 $parent_id 的所有子分类数据 // ... return $categories; } // 输出所有分类 getCategoryTree(); ``` 2. 非实现无限分类实现无限分类的方法是使用堆栈(或队列)来处理分类数据。具体实现步骤如下: (1)从数据库中获取所有分类数据,并按照父子关系组织为一个数组。 (2)将根分类(即 parent_id 为 0 的分类)入栈。 (3)从堆栈中取出一个分类,并输出该分类的名称。 (4)将该分类的所有子分类依次入栈。 (5)重复步骤(3)和(4),直到堆栈为空。 下面是一个非实现无限分类的示例代码: ```php function getCategoryTree() { $categories = getCategoryByParentId(0); $stack = array(); foreach ($categories as $category) { array_push($stack, array('category' => $category, 'level' => 0)); } while (count($stack) > 0) { $current = array_pop($stack); echo str_repeat('-', $current['level']) . $current['category']['name'] . '<br>'; $children = getCategoryByParentId($current['category']['id']); foreach ($children as $child) { array_push($stack, array('category' => $child, 'level' => $current['level'] + 1)); } } } function getCategoryByParentId($parent_id) { // 从数据库中获取 $parent_id 的所有子分类数据 // ... return $categories; } // 输出所有分类 getCategoryTree(); ``` 以上就是php实现无限分类的两种方法。从实现难度和效率上来看,归方法更简单,但对于数据量较大的情况,可能会导致栈溢出。非归方法虽然复杂一些,但可以处理更大的数据量。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值