当前位置:网站首页>Stream stream is used for classification display.
Stream stream is used for classification display.
2022-07-23 14:13:00 【Ozawa can't Java】
1: First , First, let's look at my database .
You can see in my database , The first level classification is parent_id yes 0, The secondary classification is parent_id It's his parent id
2: The goal is
My goal , That is to integrate the primary classification and secondary classification through flow programming , That is to put the secondary classification into the primary classification children Properties of the , And then it goes back to the front end .

3: terms of settlement
(1) The first one is
The idea is , Create two separate classes , One First level classification , One Secondary classification ,, And then use MP, , respectively, Get all First level classification And all Secondary classification , ad locum , Because all the fields in the table are returned in the background , Instead of the fields we want , So you need to assign values separately .
// First level classification
@Data
@Accessors(chain = true)
public class OneSubject {
private String id;
private String title;
// A primary classification has multiple secondary classifications
private List<TwoSubject> children=new ArrayList<>();
}
// Secondary classification
@Data
@AllArgsConstructor
@NoArgsConstructor
public class TwoSubject {
private String id;
private String title;
}Java Code implementation
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {
// Put the primary classification and secondary classification together
@Override
public List<OneSubject> getAllOneTwoSubject() {
// Query all first level classifications parentid=0
QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
wrapperOne.eq("parent_id","0");
List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperOne);
// Query all secondary classifications parentid!=0
QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
wrapperOne.ne("parent_id","0");
List<EduSubject> twoSubjectList = baseMapper.selectList(wrapperTwo);
// establish list aggregate , Used to store the final encapsulated data
List<OneSubject> finalSubjectList=new ArrayList<>();
// Package level 1 Classification
// Find all the first level classifications list A collection of traverse , Get the classification object of each level , Get the value of each primary classification object
// Package to the required list In the assembly
for (int i = 0; i < oneSubjectList.size(); i++) {
EduSubject eduSubject = oneSubjectList.get(i);
OneSubject oneSubject = new OneSubject();
// oneSubject.setId(eduSubject.getId());
// oneSubject.setTitle(eduSubject.getTitle());
// hold eduSubject Copy the value to the corresponding oneSubject Inside the object , The properties in the two objects are the same, and the corresponding automatic assignment
BeanUtils.copyProperties(eduSubject,oneSubject);
// Loop through and query all the secondary classifications in the primary classification
// establish list The set encapsulates the secondary classification of each primary classification
List<TwoSubject> twoFinalSubjectList=new ArrayList<>();
// Traverse the secondary classification list aggregate
for (int j = 0; j < twoSubjectList.size(); j++) {
EduSubject tSubject = twoSubjectList.get(j);
// If the secondary classification parentid And primary classification id equally , Just add it to the primary classification
if(tSubject.getParentId().equals(eduSubject.getId())){
TwoSubject twoSubject = new TwoSubject();
BeanUtils.copyProperties(tSubject,twoSubject);
twoFinalSubjectList.add(twoSubject);
}
}
// Put all the secondary classifications below the first level into the first level
oneSubject.setChildren(twoFinalSubjectList);
finalSubjectList.add(oneSubject);
}
return finalSubjectList;
}
}(2) The second kind
The second method uses streaming programming , That is our theme today .
Ideas : There is no need to create a type here , In the receive field returned by the database Add a new one Field . Create a Stream flow , And then in map Method to set its secondary classification , That is to say children. Define a method here , It is to obtain all the secondary classifications under a primary classification , Then return to all secondary classifications , Then add it to the first level of classification to get children Properties of the .
@TableField(exist = false)// This field does not exist in the database
private List<EduSubject> children;Java Code implementation
// Put the primary classification and secondary classification together
@Override
public List<EduSubject> getAllOneTwoSubject() {
// Query all first level classifications parentid=0
QueryWrapper<EduSubject> wrapperOne = new QueryWrapper<>();
wrapperOne.eq("parent_id", "0");
List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperOne);
// Query all secondary classifications parentid!=0
QueryWrapper<EduSubject> wrapperTwo = new QueryWrapper<>();
wrapperOne.ne("parent_id", "0");
List<EduSubject> twoSubjectList = baseMapper.selectList(wrapperTwo);
// At this time, we have got the first level classification (oneSubjectList) And secondary classification (twoSubjectList),
//1: Adopt flow programming to encapsulate the data in the secondary classification into the primary classification children in
oneSubjectList.stream()
.map(sub ->{
return sub.setChildren(getChildren(sub.getId(),twoSubjectList));
})
.collect(Collectors.toList());
return oneSubjectList;
}
// // Judge the secondary classification in the primary parent class
@Override
public List<EduSubject> getChildren(String oneSubject, List<EduSubject> eduSubject) {
return eduSubject.stream().filter(res -> {return res.getParentId().equals(oneSubject);}).collect(Collectors.toList());
}边栏推荐
- ThreadLocal 面试夺命11连问
- Excitation generator, monitor
- Remember that a vulnhub target plane exercise successfully won the root permission
- 头部产品创收25亿,SLG赛道也被黑产盯上了
- 考研题库小程序中如何实现打开考研思维导图pdf
- 200 lines of code, in-depth analysis of the principle and implementation of dynamic calculation diagram
- Detailed introduction of RIP
- Notes on animal farm
- Differences between Xiaomi 12s pro and Xiaomi 12pro Tianji version configuration comparison between the two
- 第十一天笔记
猜你喜欢

Overlayfs source code parsing

Where does pytorch work?

Medium range

容器网络原理

英特尔赛扬7300性能怎么样?相当于什么水平级别
![[laser principle and application -7]: semiconductor refrigeration sheet and Tec thermostat](/img/c8/e750ff7c64e05242eac7b53b84dbae.png)
[laser principle and application -7]: semiconductor refrigeration sheet and Tec thermostat

200 lines of code, in-depth analysis of the principle and implementation of dynamic calculation diagram

Talent evaluation Core i7 12850hx and i7 12700h which to choose

天玑820相当于骁龙多少处理器 天玑1100相当于骁龙多少 天玑820怎么样

第七天笔记
随机推荐
SDF refraction and reflection effect recording
达人评测酷睿i7 12850hx和i7 12700h选哪个
Golang remote server debugging
Head products generated 2.5 billion yuan, and SLG track was also targeted by black products
Detailed explanation of knapsack problem
Notes on animal farm
What is Tianji 920 equivalent to a snapdragon? How much is Tianji 920 equivalent to a snapdragon? How about Tianji 920
Creo 9.0 如何快速修改CAD坐标系?
How to judge whether an object is empty
Overlayfs source code parsing
overlayfs源代码解析
Reinforcement learning -- understanding point of strategy gradient
[wechat applet] case - local life
Notes on key vocabulary of the original English book biography of jobs (15) [chapter four]
Notes on the fifth day
Static comprehensive experiment (HCIA)
canvas橡皮擦功能
使用Stream流来进行分类展示。
Is there a big gap between core i5 12490f and i5 12600K
OSPF comprehensive experiment