7

Is there a way to do something like this:

ArrayList<String>.removeAll(ArrayList<Integer>)

With the ArrayList<Integer> being the indices that I want deleted. I know that I could iterate through the indices list and use remove(index), but I was wondering if there is a one-command way of doing so.

I know how to put this iteration into one line, my question is, if there is a way implemented by oracle.

2
  • There is no such built-in method. However, you can easily create such a method by simply iterating over the indices, calling the ordinary remove method for each index individually. All possible answers to your question will show such an approach. Commented Mar 14, 2018 at 16:54
  • @Zabuza thank you very much :) Commented Mar 14, 2018 at 16:55

3 Answers 3

5

You can use a Stream to iterate through the indices to remove. However, take care to remove the highest index first, to avoid shifting other elements to remove out of position.

public void removeIndices(List<String> strings, List<Integer> indices)
{
     indices.stream()
         .sorted(Comparator.reverseOrder())
         .forEach(strings::remove);
}

For removing from a list of Strings this will work, calling the proper remove(int) method. If you were to try this on a List<Integer>, then you will have to avoid calling remove(E) by calling .mapToInt(Integer::intValue) before calling forEach.

Sign up to request clarification or add additional context in comments.

Comments

3

You can use Java 8 Streams.

For example:

IntStream.of(7,6,5,2,1).forEach(i->list.remove(i));

If the indices are given as a List<Integer>, you can do:

indexList.stream().mapToInt(Integer::intValue).forEach(i->list.remove(i));

Note that I preferred to use an IntStream and not a Stream<Integer>, since if you use a Stream<Integer> for the indices, if the list from which you wish to remove elements is itself a List<Integer>, calling remove(Integer) will remove the element whose value is that Integer, not the element whose index is that Integer.

6 Comments

Isn't this just another way of iterating through the second arrayList?
@AzogtheDebugger I don't know how you expect to do it otherwise. It would iterate anyway at some point of the stack.
You probably want to have the list in reverse numerical order, because index 2 will point to a different item after index 1 is removed.
I might as well do indexList.forEach((index) -> { list.remove(i); }) at this point, no need to use a stream
@AzogtheDebugger I preferred to use int indices instead of Integer indices, to make the solution more general (otherwise it won't work if list was a List<Integer>).
|
0

I still get warnings with the above by @rgettman. To avoid the warnings and to make sure you are using the right remove method you can do this. I know some people hate lambdas but I think this is clearer than the extra mapToInt

    public void removeIndices(List<OtherObject> other, List<Integer> indices)
    {
       indices.stream()
             .sorted(Comparator.reverseOrder())
             .forEach(i->other.remove(i.intValue()));
    }  

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.