java商品类的无限递归展示
来源:互联网 发布:php拼接字符串 编辑:程序博客网 时间:2024/05/23 17:13
java商品类或者雇员的的无限递归展示
20170630
java树的无限层级递归,也可以理解为商品类的无限层级递归,其实思想是一样的,
但是在实际项目中,这里的数据源文件可能是从数据库中提取
给定一个文件,文件中数据为下面所示:
liu,108391,123457zhao,123457,123139xin,610123,123457hu,507123,123457zhang, 123139, xu,123422,123457qian,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<Employee> employeeSetAll= new HashSet(); public static final String SPACE = " "; public static void readFileTest(String fileName,Set<Employee> 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<Employee> findChildEmployee(Set<Employee> employeeSet,Integer id,int level){ Iterator<Employee> it = employeeSetAll.iterator(); List<Employee> employeeList = new ArrayList<Employee>(); 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;i<level;i++){ System.out.print(SPACE); } System.out.println(e.getId()); findChildEmployee(employeeSet,e.getId(),level+1); } 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<Employee> employeeSet= new HashSet(); StringBuffer sb = new StringBuffer(); findChildEmployee(employeeSet,123139,0); }}
在happymmall也使用了类似的层级结构,但是数据源是从MySQl中获取的。
阅读全文
0 0
- java商品类的无限递归展示
- 无限递归树展示
- 商品展示的案例
- java代码实现商品类别的无限级分类显示
- 商品展示案例的ShopShowDemo
- 简单的商品列表展示
- 商品展示——实体类和适配器的创建
- php实战之使用递归读取商品无限分类
- 商品展示
- 商品展示
- 商品展示
- 商品展示
- 商品展示
- 商品展示
- 商品展示
- 商品展示
- 商品展示
- 商品展示
- 如何用微服务重构应用程序
- Oracle 9i RMAN恢复过程中,归档日志找不到.
- mac Apache+php环境开发
- 【近期安排】
- kamailio gdb调试
- java商品类的无限递归展示
- 【Linux】CentOS(6.8)更新yum源 -->(阿里源<ali>)
- linux crontab定时执行命令
- eclipse 修改字符集
- 【Nova】架构学习
- iOS 如何自定义UISearchBar 中textField的高度
- 通过cookielib获取cookies
- Swift3.0学习之ScrollView的简单使用
- 很详细的unity脚本函数调用