If I have a List
containing objects like
new MyObj("some"),
new MyObj("foo"),
new MyObj("bar"),
new MyObj("xyz");
and want to filter it with Java 8 streams after match some condition, say
myObj.prop = "foo";
how would that be accomplished?
The result of the above example should be
new MyObj("some"),
new MyObj("foo")
That would be the "traditional" way:
List<MyObj> results = new ArrayList<>();
for (MyObj myObj : myObjList) {
if (!myObj.prop.equals("foo")) {
results.add(myObj);
} else {
results.add(myObj);
break;
}
}
It is not exaclty a duplicate of Limit a stream by a predicate because that does not include the matched element. However, I should be able to adapt the takeWhile operation.