如何将带有List <entite>的Entite列表转换为treeSet

时间:2016-12-20 18:15:18

标签: java collections

创建inernet商店,请帮忙 我有类别实体,其中包含List,我需要在json中发送此树,但是使用dao它会找到所有类别,而我的json看起来像这样

[
    {
        "id": 1,
        "title": "Phones & Accessories",
        "products": [],
        "subcategories": [
            {
                "id": 2,
                "title": "Mobile Phones",
                "products": [],
                "subcategories": []
            },
            {
                "id": 3,
                "title": "Phone Bags & Cases",
                "products": [],
                "subcategories": []
            },
            {
                "id": 4,
                "title": "Mobile Phone Accessories",
                "products": [],
                "subcategories": []
            },
            {
                "id": 5,
                "title": "Mobile Phone Parts",
                "products": [],
                "subcategories": []
            }
        ]
    },
    {
        "id": 2,
        "title": "Mobile Phones",
        "products": [],
        "subcategories": []
    },
    {
        "id": 3,
        "title": "Phone Bags & Cases",
        "products": [],
        "subcategories": []
    },

所以类别重复,据我所知,我不需要返回一个列表,而是在我的控制器方法中返回一个treeSet。但我不明白该怎么做,如何实现可比较的方法,所以我的json没有重复的实体 控制器映射

@Controller
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @RequestMapping("/categories")
    public @ResponseBody TreeSet<Category> getCategoriesTree(){
        List<Category> list = categoryService.findAll();
        TreeSet<Category> categoryTree = new TreeSet<>(list);
        return categoryTree;
    }

    @RequestMapping("/categorieslist")
    public @ResponseBody List<Category> getCategoriesList(){
        List<Category> list = categoryService.findAll();
        TreeSet<Category> categorytree = new TreeSet<>(list);
        return list;
    }
}

实体

@Entity
@Getter
@Setter
public class Category  implements Comparable<Category> {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotNull
    private String title;

    @OneToMany
    @JoinTable(name = "category_product", joinColumns = { @JoinColumn(name = "category_id") }, inverseJoinColumns = { @JoinColumn(name = "product_id") })
    private Set<Product> products;

    @OneToMany(cascade = CascadeType.MERGE )
    @JoinTable(name = "category_subcategory", joinColumns = { @JoinColumn(name = "category_id") }, inverseJoinColumns = { @JoinColumn(name = "subcategory_id") })
    private List<Category> subcategories;

    @Override
    public int compareTo(Category o) {
        if (o.getSubcategories().contains(this))  {
           return 0;
        } else return 1;

    }
}

1 个答案:

答案 0 :(得分:0)

答案更简单 我刚用空列表过滤了所有类别 并获得了Root&#39;类别

List<Category> categories = categoryService.findAll();
    List<Category> roots = categories.stream().filter(category -> !category.getSubcategories().isEmpty()).collect(Collectors.toList());
    return roots;