再帰処理リスト集合【無限レベル分類の実現】


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