java樹結構工具-TreeUtil(註解的方式實現)

樹結構工具-TreeUtil(註解的方式實現)

將有樹結構的集合封裝爲樹結構java

使用步驟:

1. 添加依賴

<dependency>
        <groupId>com.github.appundefined</groupId>
        <artifactId>treeUtils</artifactId>
        <version>1.0.1-RELEASE</version>
    </dependency>

2. 在須要轉換爲樹結構的對象字段上添加核心註解3個

@TreeElement(name = "id")          
private String id;                 
@TreeElement(name = "pid")         
private String pid;                            
@TreeElement(name = "children")   
private List<Object> children = new ArrayList<>();(注:沒有List集合須要添加一個)

3. 調用

List trees = TreeUtils.ListToTree(objects);

4. 源碼地址

https://github.com/AppUndefined/javaUtils.gitnode

5. 源碼

一個註解:TreeElementgit

package com.github.appundefined.tree;

import java.lang.annotation.*;

@Target(value= {ElementType.FIELD})
@Documented
@Retention(value = RetentionPolicy.RUNTIME)
public @interface TreeElement {
    String name();
}

一個註解:TreeTreeUtilsgithub

package com.github.appundefined.tree;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class TreeUtils {
    /** * Root node value */
    static String rootNode = "0";
    /** * Primary key id */
    static String id = "id";
    /** * Parent node id */
    static String pid = "pid";
    /** * Child node collection */
    static String children = "children";

    /** * Convert List to List Tree * @param trees t * @param <T> t * @return result * @throws IllegalAccessException result */
    public  static   <T>  List<T>   ListToTree(List<T> trees) throws IllegalAccessException {
         //Root node collection
        List<T> rootList = new ArrayList<T>();
        HashMap<Object, List<T>> pidAndTrees= new HashMap<>();
        for (final T t : trees) {
            Object value = getValue(t, pid);
            if(value!=null){
                if(pidAndTrees.get(value)!=null) {
                    pidAndTrees.get(value).add(t);
                }else{
                    pidAndTrees.put(value,new ArrayList<T>(){{this.add(t);}});
                }
                if(rootNode.equals(value)) {
                    rootList.add(t);
                }
            }
        }
        buildChilTree(rootList,pidAndTrees);
        return rootList;
    }

    /** * Get the field value corresponding to the specified annotation value of the object * @param t * @param key * @param <T> * @return * @throws IllegalAccessException */
    private static   <T>  Object getValue(T t,String key) throws IllegalAccessException {
        Field[] fields = t.getClass().getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            TreeElement treeElement = field.getAnnotation(TreeElement.class);
            if(treeElement!=null){
                String name = treeElement.name();
                if(key.equals(name)){
                    Object o = field.get(t);
                    return o;
                }
            }
        }
        return null;
    }

    /** * Recursive construction * @param currentTrees * @param trees * @param <T> * @throws IllegalAccessException */
    private static  <T> void  buildChilTree(List<T> currentTrees,    HashMap<Object, List<T>>  trees) throws IllegalAccessException {
        for (T t : currentTrees) {
            Object currentId = getValue(t, id);
            //Data exists with current id as pid
            if(trees.get(currentId)!=null){
                List list = (List) getValue(t, children);
                list.addAll(trees.get(currentId));
                buildChilTree(trees.get(currentId),trees);
            }
        }

    }

}
相關文章
相關標籤/搜索