集合多级评论

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

 

package com.tledu.practice.day_19.test;

import java.util.*;

import java.util.stream.Collectors;

/**

* @author Mr.Wang

* @date 23-3-25

*/

public class Test_01 {

/**

* 给出示例数据,包括不同的学院,每个学院有不同的专业,每个专业有不同的班,

* 现在要求用所学的集合的知识,把他们组装好,班级放到专业里,专业放到学院里

* @param args

*/

public static void main(String[] args) {

//示例数据

List<SysDept> list=new ArrayList<>();

SysDept sysDept=new SysDept(0L,null,"信息学院");

SysDept sysDept22=new SysDept(1L,null,"影视学院");

SysDept sysDept1=new SysDept(101L,0L,"软件工程");

SysDept sysDept2=new SysDept(102L,0L,"计算机科学与技术");

SysDept sysDept3=new SysDept(103L,0L,"网络工程");

SysDept sysDept4=new SysDept(501L,101L,"软件1班");

SysDept sysDept5=new SysDept(502L,101L,"软件2班");

SysDept sysDept6=new SysDept(503L,101L,"软件3班");

SysDept sysDept7=new SysDept(601L,102L,"计算机1班");

SysDept sysDept8=new SysDept(602L,102L,"计算机2班");

SysDept sysDept9=new SysDept(603L,102L,"计算机3班");

SysDept sysDept10=new SysDept(701L,103L,"网络1班");

SysDept sysDept11=new SysDept(702L,103L,"网络2班");

SysDept sysDept12=new SysDept(703L,103L,"网络3班");

list.add(sysDept);

list.add(sysDept1);

list.add(sysDept2);

list.add(sysDept3);

list.add(sysDept4);

list.add(sysDept5);

list.add(sysDept6);

list.add(sysDept7);

list.add(sysDept8);

list.add(sysDept9);

list.add(sysDept10);

list.add(sysDept11);

list.add(sysDept12);

list.add(sysDept22);

// System.out.println(list);

List<SysDept> deptTree = getDeptTree(list);

System.out.println(deptTree);

}

/**

* 演示代码

* @param list

* @return

*/

public static List<SysDept> getDeptTree(List<SysDept> list) {

Map<Long, SysDept> map = new HashMap<>();

// 获取id数组,和映射关系

//这里的stream流可替换成for循环

List<Long> idList = list.stream().map(sysDept -> {

map.put(sysDept.getDeptId(), sysDept);

return sysDept.getDeptId();

}).collect(Collectors.toList());

// map中装填了13项,k=部门id,v是自己部门对象

// idList中装填了13个id

// 最终的数组

List<SysDept> deptList = new ArrayList<>();

list.forEach(sysDept -> {

// 在这个集合中能找到它的父级

if (idList.contains(sysDept.getParentId())) {

// 获取它的父级

SysDept dept = map.get(sysDept.getParentId());

// 如果没有子节点创建一个

if (dept.getChildren() == null) {

dept.setChildren(new ArrayList<>());

}

dept.getChildren().add(sysDept);

}else{

// 如果找不到他的父级,那么说明它应该是根节点

deptList.add(sysDept);

}

});

// 如果没有放到父级数组中的数据,代表要放在第一级别

return deptList;

}

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

 

package com.tledu.practice.day_19.test;

import java.util.ArrayList;

import java.util.List;

/**

* @author Mr.Wang

* @date 23-3-25

*/

public class SysDept implements Comparable<SysDept>{

/** 部门ID */

private Long deptId;

/** 父部门ID */

private Long parentId;

/** 祖级列表 */

private String ancestors;

/** 部门名称 */

private String deptName;

/** 负责人 */

private String leader;

/** 新加属性 子部门 */

private List<SysDept> children ;

public SysDept() {

}

@Override

public String toString() {

return "SysDept{" +

"deptId=" + deptId +

", parentId=" + parentId +

", deptName='" + deptName + '\'' +

'}';

}

public SysDept(Long deptId, Long parentId, String deptName) {

this.deptId = deptId;

this.parentId = parentId;

this.deptName = deptName;

}

public Long getDeptId() {

return deptId;

}

public void setDeptId(Long deptId) {

this.deptId = deptId;

}

public Long getParentId() {

return parentId;

}

public void setParentId(Long parentId) {

this.parentId = parentId;

}

public String getAncestors() {

return ancestors;

}

public void setAncestors(String ancestors) {

this.ancestors = ancestors;

}

public String getDeptName() {

return deptName;

}

public void setDeptName(String deptName) {

this.deptName = deptName;

}

public String getLeader() {

return leader;

}

public void setLeader(String leader) {

this.leader = leader;

}

public List<SysDept> getChildren() {

return children;

}

public void setChildren(List<SysDept> children) {

this.children = children;

}

@Override

public int compareTo(SysDept o) {

return 0;

}

}

实现多级评论需要在后端和前端两个方面进行处理。 在后端,你需要定义一个评论的数据结构,可以使用递归方式来处理多级评论每个评论对象应该包含评论的内容、评论的时间、评论者的信息以及子评论列表。子评论列表也是一个评论对象的集合,可以使用 List 或者 Set 来实现。 在前端,你需要使用递归组件来展示多级评论。可以定义一个 Comment 组件来展示一个评论对象,如果这个评论对象包含子评论,那么在 Comment 组件中递归调用 Comment 组件来展示子评论。 以下是一个简单的示例代码: 后端 Java 代码: ```java public class Comment { private String content; private LocalDateTime time; private User user; private List<Comment> children; // getters and setters } ``` 前端 Vue 代码: ```vue <template> <div class="comment"> <div class="content">{{ comment.content }}</div> <div class="time">{{ comment.time }}</div> <div class="user">{{ comment.user.name }}</div> <div class="children"> <comment v-for="child in comment.children" :comment="child" :key="child.id"></comment> </div> </div> </template> <script> export default { name: "Comment", props: { comment: { type: Object, required: true } }, components: { Comment: () => import("./Comment.vue") } }; </script> ``` 在这个示例中,我们使用了递归组件 Comment 来展示多级评论。如果当前评论对象包含子评论,那么在 Comment 组件中递归调用 Comment 组件来展示子评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值