Methods 代码帮助-避免重复代码并重新排列
以下代码用于获取两种产品类别的产品类别列表Methods 代码帮助-避免重复代码并重新排列,methods,java-8,code-cleanup,Methods,Java 8,Code Cleanup,以下代码用于获取两种产品类别的产品类别列表 private List<Category> setCategory(CategoryContext categoryContext, CategoryWrapper categoryWrapper) { List<Category> categoryList = Collections.emptyList(); if(categoryWrapper.getCategoryType() == Jewell
private List<Category> setCategory(CategoryContext categoryContext, CategoryWrapper categoryWrapper) {
List<Category> categoryList = Collections.emptyList();
if(categoryWrapper.getCategoryType() == Jewellery) {
List<JewelleryWrapper> Prices = categoryWrapper.getJewelleryPrices();
if (Prices != null) {
categoryList = Prices.stream()
.filter(Price -> Price.getJPrice() != null)
.map(this::makeCategory)
.filter(Objects::nonNull)
.collect(Collectors.toList());
}
} else if (categoryWrapper.getCategoryType() == Cloth ) {
List<ClothWrapper> Prices = categoryWrapper.getClothPrices();
if (Prices != null) {
categoryList = Prices.stream()
.filter(Price -> Price.getCPrice() != null)
.map(this::makeCategoryX)
.filter(Objects::nonNull)
.collect(Collectors.toList());
}
}
其他将是如下所示:
private CategoryItem makeCategoryX(ClothWrapper Price) {
CategoryItem categoryItem = new CategoryItem();
categoryItem.setId(Price.getClothId());
categoryItem.setPrice(Price.getCPrice());
return categoryItem;
}
还有两个类别需要添加。但我觉得这些代码中存在重复。
谁能帮帮我吗 虽然我怀疑方法引用的解析,但是对不同类型的参数重载
makeCategory
是否没有帮助?一个CategoryItem
和jewelleryRapper
和ClothWrapper
类之间有什么区别?这三个类似乎只是id和价格的组合。但它有来自不同层的不同产品类别详细信息尽管我怀疑方法引用的解决方案,但重载makeCategory
以获得不同类型的参数在这里没有帮助吗?CategoryItem
还有jewelleryrapper
和ClothWrapper
课程?这三个类别似乎只是一个id和一个价格的组合,但它有来自不同层次的不同产品类别细节
private CategoryItem makeCategoryX(ClothWrapper Price) {
CategoryItem categoryItem = new CategoryItem();
categoryItem.setId(Price.getClothId());
categoryItem.setPrice(Price.getCPrice());
return categoryItem;
}