[英]to find the index number from a list based on the condition
如何在满足条件的列表中找到一个object的索引号
int index = list.indexOf(list.stream().filter(a-> a.getInfo().getDetails().getIndicator().equals("3")));
list.remove(index)
如果 Indicator 为 3,我需要获取索引值
由于您在问题代码中包含list.remove(index)
,如果您想删除满足条件的项目,请不要获取索引然后按索引删除,只需使用removeIf(...)
:
list.removeIf(a -> a.getInfo().getDetails().getIndicator().equals("3"));
可以使用IntStream
根据索引迭代列表,得到满足条件的索引 object
IntStream.range(0,list.size())
.filter(i->list.get(i).getInfo().getDetails().getIndicator().equals("3"))
.findFirst()
.orElse(-1) // return -1 as default
使用旧式迭代器,您可以:
迭代时删除匹配元素
移除所有
for(Iterator<YourType> it=items.iterator();it.hasNext();) { YourType element=it.next(); if(element.getInfo().getDetails().getIndicator().equals("3")) it.remove(); }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.