See comments for question. Remove N elements selectively (Condition is that list element matches 'remove')
List<String> mylist = new ArrayList<>();
mylist.add("remove");
mylist.add("all");
mylist.add("remove");
mylist.add("remove");
mylist.add("good");
mylist.add("remove");
// Remove first X "remove".
// if X is 2, then result list should be "all, remove, good, remove"
// Use java 8 features only, possibly single line code.
// Please don't answer with looping, iterating, if conditions etc.
// Answer should use JDK 8 new features.
IntStream.range(0,x).forEach(i -> mylist.remove("remove"));, but it's going to be unnecessarilyO(N*x)and just a bad idea.Collections.nCopies(x, "remove").forEach(mylist::remove);, but @Misha’s comment applies to this as well.