Question: 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.

```

Answer: 
How about this:

```
List<String> filter(List<String> mylist, int x){
    AtomicInteger index = new AtomicInteger(0);
    mylist.removeIf(p -> p.equals("remove") && index.getAndIncrement() < x);
    return myList;
}

```

With x=0, it prints:

> 
> [remove, all, remove, remove, good, remove]
> 
> 
> 

With x=1, it prints:

> 
> [all, remove, remove, good, remove]
> 
> 
> 

With x=2, it prints:

> 
> [all, remove, good, remove]
> 
> 
> 

With x=3, it prints:

> 
> [all, good, remove]
> 
> 
>
try this:

```
List<String> mylist = new ArrayList<>();
    mylist.add("remove");
    mylist.add("all");
    mylist.add("remove");
    mylist.add("remove");
    mylist.add("good");
    mylist.add("remove");
    IntStream.range(0,2).forEach(a-> mylist.remove("remove"));

    System.out.println(mylist);

```