例如,数组列表包含4个对象。例如:object 1,object2,child1_object1,child1_object1。对象1内部包含child1_object1,child1_object1如下
现在我想要算法从主列表中删除子对象。请帮忙
答案 0 :(得分:0)
您的数据结构看起来像 Map ,一个映射对象到List。 在此处查看Guava MultiMap https://google.github.io/guava/releases/21.0/api/docs/com/google/common/collect/ListMultimap.html
如果您不想要依赖,可以执行以下操作:
int parentIndex = parentList.indexOf(parentObject);
if(parentIndex != -1){
List<Object> children = parentList.get(parentIndex);
if(children != null){
children.remove(childIndex);
}
}
但是,我建议您使用Map指向儿童。
parentList.indexOf(parentObject);
进行线性搜索,即O(n),因为如果您的父列表非常大,可能会出现问题。