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]
> 
> 
>
A not entirely serious solution with streams which avoids state (updated): 

```
        IntStream.range(0, mylist.size()) //1
             .mapToObj(x -> new AbstractMap.SimpleEntry<>(x, mylist.get(x))) //2
             .collect(Collectors.partitioningBy(x->x.getValue().equals("remove"))) //3
             .entrySet()
             .stream()
             .flatMap( x->x.getKey()?x.getValue().stream().skip(2):x.getValue().stream()) //4
             .sorted(Map.Entry.comparingByKey()) //5
             .map(Map.Entry::getValue) //6
             .collect(Collectors.toList());

```

1. An IntStream is used to preserve the original sorting of the list.
2. Tuples are created
3. The list is partitionized depending on if the string equals "remove"
4. only elements from the list containing "remove" are skipped
5. the original sorting if restored
6. finally, the orignal Strings are restored from the Tuple

Since the list is wrapped and unwrapped two times, partitionized and sorted, it is unfortunately not a very efficient solution. So probably, a simple for-loop is more efficient than a stream-based solution.