①. Stream.sorted字段排序
-
①. sorted() 默认使用自然序排序, 其中的元素必须实现Comparable接口 -
②. sorted(Comparator<? super T> comparator) :我们可以使用lambada来创建一个Comparator实例。可以按照升序或着降序来排序元素 -
③. 常用方法展示:
list.stream().sorted()
list.stream().sorted(Comparator.reverseOrder())
list.stream().sorted(Comparator.comparing(Student::getAge))
list.stream().sorted(Comparator.comparing(Student::getAge).reversed())
List<User> lists = Lists.newArrayList();
lists.add(new User(1, 24, "张三", new Info("7000")));
lists.add(new User(2, 22, "李四", new Info("8500")));
lists.add(new User(3, 24, "王五", new Info("9000")));
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
List<User> userList = lists.stream()
.sorted(Comparator.comparing(User::getAge))
.collect(Collectors.toList());
List<Integer> numberList = numbers.stream()
.sorted(Comparator.comparing(Integer::intValue))
.collect(Collectors.toList());
List<User> userList = lists.stream()
.sorted(Comparator.comparing(User::getAge).reversed())
.collect(Collectors.toList());
List<Integer> numberList = numbers.stream()
.sorted(Comparator.comparing(Integer::intValue).reversed())
.collect(Collectors.toList());
②. thenComparing多字段排序
List<User> userList = lists.stream()
.sorted(Comparator.comparing(User::getAge).thenComparing(User::getSalary))
.collect(Collectors.toList());
List<User> userList = lists.stream()
.sorted(Comparator.comparing(User::getAge).thenComparing(User::getSalary,Comparator.reverseOrder()))
.collect(Collectors.toList());
③. 三级分类树状展示
[
{
"categoryType": "LIBRARY",
"childList": [
{
"categoryType": "LIBRARY",
"code": "200200",
"id": "5f1a559e3a36990001bf24e1",
"modifyTime": 1598593435000,
"name": "清热解毒",
"nodeLevel": 1,
"parentCode": "100017",
"shopCode": "YD-5e79a51c28a3200001d9e719",
"shopMerchant": "5e79a51c28a3200001d9e719",
"sort": 0,
"status": true
},
{
"categoryType": "LIBRARY",
"code": "200197",
"id": "5f1a559e3a36990001bf24e2",
"imgUrl": "",
"modifyTime": 1598593435000,
"name": "感冒药",
"nodeLevel": 1,
"parentCode": "100017",
"shopCode": "YD-5e79a51c28a3200001d9e719",
"shopMerchant": "5e79a51c28a3200001d9e719",
"sort": 1,
"status": true
},
{
"categoryType": "LIBRARY",
"code": "200291",
"id": "5f1a559e3a36990001bf24e3",
"modifyTime": 1598593435000,
"name": "其他",
"nodeLevel": 1,
"parentCode": "100017",
"shopCode": "YD-5e79a51c28a3200001d9e719",
"shopMerchant": "5e79a51c28a3200001d9e719",
"sort": 2,
"status": true
},
{
"categoryType": "LIBRARY",
"code": "200199",
"id": "5f1a559e3a36990001bf24e5",
"modifyTime": 1598593436000,
"name": "消炎药",
"nodeLevel": 1,
"parentCode": "100017",
"shopCode": "YD-5e79a51c28a3200001d9e719",
"shopMerchant": "5e79a51c28a3200001d9e719",
"sort": 4,
"status": true
}
],
"code": "100017",
"id": "5f1a559e3a36990001bf24e6",
"imgUrl": "",
"modifyTime": 1650966344000,
"name": "常备用药",
"nodeLevel": 0,
"shopCode": "YD-5e79a51c28a3200001d9e719",
"shopMerchant": "5e79a51c28a3200001d9e719",
"sort": 0,
"status": true
}
]
public List<GoodsCategory> getGoodsCategoryList(......) {
List<GoodsCategory> parentCategorieList = collect.stream().filter((entity) -> {
return entity.getParentCode() == null;
}).map((item) -> {
item.setChildList(getChilder(item, collect));
return item;
})
.sorted((menu1,menu2)->{
return menu1.getParentCode()==null?0:menu1.getParentCode().length()-(menu2.getParentCode()==null?0:menu2.getParentCode().length());
})
.sorted(Comparator.comparingInt(GoodsCategory::getSort).thenComparing(GoodsCategory::getModifyTime,Comparator.reverseOrder()))
.collect(Collectors.toList());
return parentCategorieList;
private List<GoodsCategory> getChilder(GoodsCategory root, List<GoodsCategory> goodsCategoryList) {
List<GoodsCategory> collect = goodsCategoryList.stream()
.filter(entity -> {
return root.getCode().equals(entity.getParentCode());
}).map((item) -> {
if(!CollectionUtils.isEmpty(getChilder(item, goodsCategoryList))){
item.setChildList(getChilder(item, goodsCategoryList));
}
return item;
})
.sorted((menu1,menu2)->{
return menu1.getParentCode()==null?0:menu1.getParentCode().length()-(menu2.getParentCode()==null?0:menu2.getParentCode().length());
})
.sorted(Comparator.comparingInt(GoodsCategory::getSort).thenComparing(GoodsCategory::getModifyTime,Comparator.reverseOrder()))
.collect(Collectors.toList());
return collect;
}
④. 三级分类树状展示2
- ②. 使用java8新特性查找三级联洞并封装成树形结构
@RestController
@RequestMapping("product/category")
public class CategoryController {
@Autowired
private CategoryService categoryService;
@RequestMapping("/list/tree")
public R list(){
List<CategoryEntity> entities = categoryService.listWithTree();
return R.ok().put("data", entities);
}
}
@Override
public List<CategoryEntity> listWithTree() {
List<CategoryEntity> entities = baseMapper.selectList(null);
List<CategoryEntity> level1Menus = entities.stream().filter(categoryEntity ->
categoryEntity.getParentCid() == 0
).map((menu)->{
menu.setChildren(getChildrens(menu,entities));
return menu;
}).sorted((menu1,menu2)->{
return (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort());
}).collect(Collectors.toList());
return level1Menus;
}
private List<CategoryEntity> getChildrens(CategoryEntity root,List<CategoryEntity> all){
List<CategoryEntity> children = all.stream().filter(categoryEntity -> {
return categoryEntity.getParentCid() == root.getCatId();
}).map(categoryEntity -> {
categoryEntity.setChildren(getChildrens(categoryEntity,all));
return categoryEntity;
}).sorted((menu1,menu2)->{
return (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort());
}).collect(Collectors.toList());
return children;
}
@Data
@TableName("pms_category")
public class CategoryEntity implements Serializable {
private static final long serialVersionUID = 1L;
@TableId
private Long catId;
private String name;
private Long parentCid;
private Integer catLevel;
@TableLogic(value = "1",delval = "0")
private Integer showStatus;
private Integer sort;
private String icon;
private String productUnit;
private Integer productCount;
@TableField(exist = false)
private List<CategoryEntity>children;
}
|