集合的父子类关系

 

一.Collection的父子类关系
1.Iterable(java集合的顶级接口之一)——实现此接口的类可以使用新的for循环

2.Iterator(接口,遍历器)ListIterator
方法:hasNext();next();remove();

3.Collection(接口,继承Iterable)
方法:
add();addAll();remove();removeAll();contains();containsAll();
size();isEmpty();toArray();clear();iterator();

4.AbstractCollection(抽象类,继承Collection并重写实现里面的方法)

二.List的父子类关系
1.List(接口,继承Collection并包含里面的所有方法)——可重复,并且有序
方法(可以通过下标操作元素):
get(i);set(i,p);add(i,p);remove(i);
indexOf(p);lastIndexOf();subList(i,j);

2.工具类Collections——为List提供排序方法
sort(list);——返回值int型
Collections.sort(list,new Comparator<String>() {
public int compare(String o1,String o2) {
return o1.length()-o2.length();
}
});

3.AbstractList(抽象类,实现接口List并重写实现里面的方法,继承AbstractCollection)

4.ArrayList(类,继承AbstractList并重写实现里面的方法)

三.队列Queue的父子类关系——遵循“先进先出”原则
1.Queue(接口,继承Collection)
方法:
offer(p);poll();peek();

2.AbstractSequentialList(抽象类,继承AbstractList)

3.LinkedList(类,继承AbstractSequentialList)

四.双端队列Deque的父子类关系
1.Deque(接口,继承Queue并重写实现里面的方法)
方法:
offerFirst()——push()——入栈;pollFirst()——pop()——出栈;
offerLast();pollLast();
peekFirst();peekLast();

2.AbstractSequentialList(抽象类,继承AbstractList)

3.LinkedList(类,继承AbstractSequentialList)

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
如果 list 集合内包含多个父级和其对应的子级,那么你需要以不同的父级为起点,逐个遍历子级,将其添加到对应的父级的 children 属性中,最终得到一个以所有父级为根节点的树形结构数组。 具体步骤如下: 1. 遍历 list 集合,找到所有的父级结构。 2. 对于每个父级结构,递归查找其子结构,并将子结构添加到父结构的 children 属性中。 3. 递归查找子结构的子结构,将其添加到对应的父结构的 children 属性中。 示例代码如下: ``` function buildTree(data) { let parents = data.filter(item => !item.parent_id); let children = data.filter(item => item.parent_id); let findChildren = (parent) => { parent.children = children.filter(item => item.parent_id === parent.id); parent.children.forEach(child => findChildren(child)); }; parents.forEach(parent => findChildren(parent)); return parents; } // 调用示例 let data = [ { id: 1, parent_id: null, name: 'A' }, { id: 2, parent_id: 1, name: 'B' }, { id: 3, parent_id: 1, name: 'C' }, { id: 4, parent_id: 2, name: 'D' }, { id: 5, parent_id: 3, name: 'E' }, { id: 6, parent_id: 4, name: 'F' }, { id: 7, parent_id: null, name: 'G' }, { id: 8, parent_id: 7, name: 'H' }, { id: 9, parent_id: 8, name: 'I' }, ]; let tree = buildTree(data); console.log(tree); ``` 运行结果如下: ``` [ { "id": 1, "parent_id": null, "name": "A", "children": [ { "id": 2, "parent_id": 1, "name": "B", "children": [ { "id": 4, "parent_id": 2, "name": "D", "children": [ { "id": 6, "parent_id": 4, "name": "F" } ] } ] }, { "id": 3, "parent_id": 1, "name": "C", "children": [ { "id": 5, "parent_id": 3, "name": "E" } ] } ] }, { "id": 7, "parent_id": null, "name": "G", "children": [ { "id": 8, "parent_id": 7, "name": "H", "children": [ { "id": 9, "parent_id": 8, "name": "I" } ] } ] } ] ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

linsa_pursuer

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值