/**
* 查询所有分类,按顺序显示
* 显示出树状
* @return
*/
public List<DealCategory> getCategories() {
List<DealCategory> dealCategories = getAllWithoutDeleted();//从缓存或数据库中查询全部
//JDK8的stream处理,把根分类区分出来
List<DealCategory> roots = dealCategories.stream().filter(dealCategory -> (dealCategory.getParentId() == 0)).collect(Collectors.toList());
//对跟分类进行排序
roots.sort(new Comparator<DealCategory>() {
@Override
public int compare(DealCategory o1, DealCategory o2) {
return o1.getOrderNum() > o2.getOrderNum() ? 1 : -1;
}
});
//把非根分类区分出来
List<DealCategory> subs = dealCategories.stream().filter(dealCategory -> (dealCategory.getParentId() != 0)).collect(Collectors.toList());
//递归构建结构化的分类信息
roots.forEach(root -> buildSubs(root, subs));
return roots;
}
/**
* 递归构建
* @param parent
* @param subs
*/
private void buildSubs(DealCategory parent, List<DealCategory> subs) {
List<DealCategory> children = subs.stream().filter(sub -> (sub.getParentId() == parent.getId())).collect(Collectors.toList());
if (!CollectionUtils.isEmpty(children)) {//有子分类的情况
parent.setChildren(children);
children.forEach(child -> buildSubs(child, subs));//再次递归构建
}
}
點(diǎn)擊查看更多內(nèi)容
2人點(diǎn)贊
評(píng)論
評(píng)論
共同學(xué)習(xí),寫下你的評(píng)論
評(píng)論加載中...
作者其他優(yōu)質(zhì)文章
正在加載中
感謝您的支持,我會(huì)繼續(xù)努力的~
掃碼打賞,你說多少就多少
贊賞金額會(huì)直接到老師賬戶
支付方式
打開微信掃一掃,即可進(jìn)行掃碼打賞哦