数据类型的比较
今天做个需求时:就是简单的三级分类,分类代码如下:
@Override
public List<CategoryEntity> queryListTree(Map<String, Object> params) {
List<CategoryEntity> categoryEntities = baseMapper.selectList(null);
//System.out.println(categoryEntities);
List<CategoryEntity> entityList = categoryEntities.stream().filter(categoryEntity -> categoryEntity.getParentCid() == 0)
.map(categoryEntity -> {
// 得到孩子
categoryEntity.setChildren(getChildren(categoryEntities, categoryEntity));
return categoryEntity;
}).collect(Collectors.toList());
System.out.println(entityList);
return entityList;
}
private List<CategoryEntity> getChildren(List<CategoryEntity> categoryEntities,
CategoryEntity category){
List<CategoryEntity> collect = categoryEntities.stream().filter(entity ->{
return entity.getParentCid() == category.getCatId();
}).map(entity -> {
entity.setChildren(getChildren(categoryEntities, entity));
return entity;
}).collect(Collectors.toList());
return collect;
}
return entity.getParentCid() == category.getCatId();里面这段代码,id是Long类型,不属于基本类型,这里出了大bug,在添加分类时,发现这里比较是false,后来经过debug知道这里出了问题。
public static Long valueOf(long l) {
final int offset = 128;
if (l >= -128 && l <= 127) { // will cache
return LongCache.cache[(int)l + offset];
}
return new Long(l);
}
数据在-128到127才会自动拆包为基本数据类型。fuck
其实这里知识点很简单,但是写业务时很容易忘记,最好所有都用equals