java商品类的无限递归展示

java商品类或者雇员的的无限递归展示

20170630

java树的无限层级递归,也可以理解为商品类的无限层级递归,其实思想是一样的,
但是在实际项目中,这里的数据源文件可能是从数据库中提取

给定一个文件,文件中数据为下面所示:

liu,108391,123457
zhao,123457,123139
xin,610123,123457
hu,507123,123457
zhang, 123139,            
xu,123422,123457
qian,123456,123422

数据表示为: employeeName,employeeId, employee boss id.

注意: 1.有些人没有boss id ; 2.注意空格的处理

问题:给定一个人的,打印出他的下属ID的层级结构

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

class Employee{

    public Employee(String name, Integer id, Integer parentId) {
        super();
        this.name = name;
        this.id = id;
        this.parentId = parentId;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Integer getId() {
        return id;
    }
    @Override
    public String toString() {
        return "Employee [name=" + name + ", id=" + id + ", parentId="
                + parentId + "]";
    }
    public void setId(Integer id) {
        this.id = id;
    }
    public Integer getParentId() {
        return parentId;
    }
    public void setParentId(Integer parentId) {
        this.parentId = parentId;
    }
    private String name;
    private Integer id;
    private Integer parentId;

}
public class FileReaderTest {

    public static final Set employeeSetAll= new HashSet();  
    public static final String SPACE = " ";

    public static void readFileTest(String fileName,Set employeeSet) throws IOException{
        int count                           = 0;
        BufferedReader br                   = null;
        FileReader fileReader               = new FileReader(fileName);
        if(fileReader != null){
            try (BufferedReader br1 = new BufferedReader(fileReader)) {
                String line;
                while ((line = br1.readLine()) != null) {
                    String [] dataArray = line.split(",");
                    Employee e = null;
                    if(dataArray.length >= 3){
                         e = new Employee(dataArray[0],Integer.parseInt(dataArray[1].trim()),Integer.parseInt(dataArray[2].trim()));
                    }else{
                        e = new Employee(dataArray[0],Integer.parseInt(dataArray[1].trim()),null);
                    }
                    employeeSet.add(e);
                }
            }
        }else{
            System.out.println("can not find the file");
        }
    }


    /**
     * ????????????id???
     * @return
     */
    public static  Set findChildEmployee(Set employeeSet,Integer id,int level){

        Iterator it = employeeSetAll.iterator();
        List employeeList = new ArrayList();
        while(it.hasNext()){
            Employee e = it.next();
            Integer parentIdValue= e.getParentId();
            if(id.equals(e.getParentId())){
                employeeList.add(e);
            }

            if(e.getId().intValue()==id.intValue()){
                employeeSet.add(e);
            }
        }
        for(Employee e:employeeList){
            for(int i=0;i1);
        }
        return  employeeSet;
    }

    public static void main(String[] args) {    
        try {
            readFileTest("data.txt",employeeSetAll);
        } catch (IOException e) {
            e.printStackTrace();
        }
        for(Employee e : employeeSetAll){
            System.out.println(e);
        }
        Set employeeSet= new HashSet();

        StringBuffer sb = new StringBuffer();
        findChildEmployee(employeeSet,123139,0);
    }
}

在happymmall也使用了类似的层级结构,但是数据源是从MySQl中获取的。

你可能感兴趣的:(java基础)