I have a list of strings ( partList
) which is a fraction part of another list of strings ( completeList
) and i want to process an object (through processObj()
) within a for loop in a way that the current element of the partList will be retracted from the completeList: when the current iteration is on an element of partList then the processing of the object will involve that element and the rest of elements from completeList i do it like so for now:
for (String el: partList) {
completeList.remove(el);
//process the target object using as parameters el and the rest of the complete list except el...
processObj(el,completeList);
completeList.add(el);
}
Is it the right way of doing it? Thanks for the enlightenment.
I'm not sure the purpose of removing then adding back to the same list, but you can use a Predicate
to accept and process certain values.
Predicate<String> accept = (s) -> {
return true; // accepts all strings; you could use partList.contains(s) here, or !s.equals(el)
}
completeList.stream()
.filter(accept.negate()) // Inverse the predicate to implement "except"
.forEach(processObj);
Replace forEach
with map
if you want to modify the stream values, then you can collect()
to get data back to a list.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.