private List<ProductCategory> recursivProductCategoryTreeList(List<ProductCategory> allProductCategoryList, ProductCategory p, List<ProductCategory> temp) { if (temp == null) { temp = new ArrayList<ProductCategory>(); } for (ProductCategory productCategory : allProductCategoryList) { ProductCategory parent = productCategory.getParent(); if ((p == null && parent == null) || (productCategory != null && parent == p)) { temp.add(productCategory); if (productCategory.getChildren() != null && productCategory.getChildren().size() > 0) { recursivProductCategoryTreeList(allProductCategoryList, productCategory, temp); } } } return temp; }