Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
根据java中的内部对象列表值从对象列表中删除元素_Java_Arraylist_Collections_Java 8_Remove If - Fatal编程技术网

根据java中的内部对象列表值从对象列表中删除元素

根据java中的内部对象列表值从对象列表中删除元素,java,arraylist,collections,java-8,remove-if,Java,Arraylist,Collections,Java 8,Remove If,如果某个内部列表属性值不满足条件,如何从列表中删除该元素。这里的技巧是,该属性本身就是一个列表,比较基于该内部列表的某个属性。请参考以下示例和帮助以代码形式填写注释部分: 对象: 驾驶员等级: 您所能做的就是在原始列表上进行流式处理,只留下满足条件的对象。它可能看起来像这样: List<Product> filtered = productList.stream() .filter( p -> p.attributeList().stream().anyMatch(

如果某个内部列表属性值不满足条件,如何从列表中删除该元素。这里的技巧是,该属性本身就是一个列表,比较基于该内部列表的某个属性。请参考以下示例和帮助以代码形式填写注释部分:

对象:

驾驶员等级:


您所能做的就是在原始列表上进行流式处理,只留下满足条件的对象。它可能看起来像这样:

List<Product> filtered = productList.stream()
      .filter( p -> p.attributeList().stream().anyMatch( a -> a.attributeId.equals(x))
      .collect(Collectors.toList()) 
在这个live中,我们实际上是在检查嵌套列表是否至少包含一个attributeId=x的对象
p、 attributeList.stream.anyMatch a->a.attributeId.equalsx

您可以执行foreach循环并删除不需要的元素。在product类中,您可以插入一个FindInnerAttribute函数在属性列表中进行搜索,如果有,则返回true

List<product> productList = new ArrayList<product>();
for(product p : productList){
    if ( p.FindInnerAttribute(x) ){
        productList.remove(p);
    }
}
当您在列表上进行迭代时,调用该列表上的remove将被中断。在最好的情况下,您会得到一个exception.productList.removiefp->p.getAttributeList.stream.mapAttribute::getAttributeId.noneMatchPredicate.isEqual​x;
List<Product> filtered = productList.stream()
      .filter( p -> p.attributeList().stream().anyMatch( a -> a.attributeId.equals(x))
      .collect(Collectors.toList()) 
List<product> productList = new ArrayList<product>();
for(product p : productList){
    if ( p.FindInnerAttribute(x) ){
        productList.remove(p);
    }
}