在开发时因为数据库存储的信息取出后为List线形结构,通过一个字段来映射其父子关系,例如评论和评论的子评论,数据库存储时通过一个parent_id 字段保存父评论的ID,来标识它与父评论的关系,但是在前端显示时需要对取出的线形数据转换为有多级数据的List集合的树状结构,所以会转换为树状结构和优化转换方法很重要的。
例子实体类
public class Home {
private long id;
private long parent_id;
private List<Home> groupHome;
public Home() {
}
public Home(long id, long parent_id) {
this.id = id;
this.parent_id = parent_id;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getParent_id() {
return parent_id;
}
public void setParent_id(long parent_id) {
this.parent_id = parent_id;
}
public List<Home> getGroupHome() {
return groupHome;
}
public void setGroupHome(List<Home> groupHome) {
this.groupHome = groupHome;
}
}
转换代码
public class Main {
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
long currentTime = System.currentTimeMillis();
List<Home> treeList = listToTree(homeList);
long runTime = System.currentTimeMillis() - currentTime;
System.out.println("run time :"+runTime);
}
}).start();
}
public static List<Home> listToTree(List<Home> homeList){
List<Home> treeList = new ArrayList<>();
//获取一级集合
for(Home h:homeList) {
if(Objects.equals(h.getParent_id(), 0l)) {
treeList.add(h);
}
}
//获取一级集合的下级或者下级集合
listToChildTree(homeList, treeList);
return treeList;
}
public static void listToChildTree(List<Home> homeList, List<Home> treeList) {
for(Home h: treeList) {
List<Home> childHomeList = h.getGroupHome();
for(Home ah:homeList) {
if(Objects.equals(h.getId(), ah.getParent_id())) {
if(childHomeList == null) {
childHomeList = new ArrayList<>();
h.setGroupHome(childHomeList);
}
childHomeList.add(ah);
}
}
if(childHomeList != null && childHomeList.size() > 0){
listToChildTree(homeList, childHomeList);
}
}
}
}
这个示例代码虽然可以实现转换,但是在数据量比较大时转换时间非常大,从上面的两层循环可以看出,其实有的数据存在重复遍历,增大了转换时间。
优化的转换代码
public class Main {
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
long currentTime = System.currentTimeMillis();
List<Home> treeList = listToTree(homeList);
long runTime = System.currentTimeMillis() - currentTime;
System.out.println("run time :"+runTime);
}
}).start();
}
public static List<Home> listToTree(List<Home> homeList){
List<Home> treeList = new ArrayList<>();
Iterator<Home> iterator = homeList.iterator();
while(iterator.hasNext()) {
Home h = iterator.next();
if(Objects.equals(h.getParent_id(), 0l)) {
treeList.add(h);
iterator.remove();
}
}
listToChildTree(homeList, treeList);
return treeList;
}
public static void listToChildTree(List<Home> homeList, List<Home> treeList) {
for(Home h: treeList) {
List<Home> childHomeList = h.getGroupHome();
Iterator<Home> iterator = homeList.iterator();
while(iterator.hasNext()) {
Home ah = iterator.next();
if(Objects.equals(h.getId(), ah.getParent_id())) {
if(childHomeList == null) {
childHomeList = new ArrayList<>();
h.setGroupHome(childHomeList);
}
childHomeList.add(ah);
iterator.remove();
}
}
if(childHomeList != null && childHomeList.size() > 0){
listToChildTree(homeList, childHomeList);
}
}
}
}
这一段示例代码对重复遍历的数据进行了删除,在转换上和没优化的转换代码比较,在应对数据量较大时,转换效率大大的提高了。
如果对您有一些意义,希望您给博主一些鼓励(点赞、关注、收藏),如果有错误欢迎大家评论。