Java Top

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE

1. Overview

In this quick tutorial, we will learn about the various ways in which we can remove an element from an array in Java using the Apache Commons Lang library.

2. Maven

Let's add the commons-lang3 dependency to our project's pom.xml file:

<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.9</version>
</dependency>

3. Removing an Element

Before we get started, let's look at what happens when we remove an element from an array without using the ArrayUtils class from the Apache Commons Lang library.

Given the array below, let's remove an element at index 2:

A simple way of doing this would be to replace the value stored at index 2 with the value stored at index 3 until we reach the end of the array:

Notice that by removing the element in the above manner, the size of the array would remain the same and the value stored at the last index would be empty. Since arrays have a fixed memory size allocated during initialization, removing an element does not adjust the size of the array.

Now let's look at the array representation when removing an element using the remove method from ArrayUtils class from Apache Commons Lang:

As we can see, the array size here is adjusted to 5 after the element is removed. The remove method creates a brand new array and copies all the values except for the value being removed.

The ArrayUtils class provides two ways of removing an element from an array. Let's look at these next.

4. Using Index as Input

The first way we can remove an element is by its index with ArrayUtils#remove:

public int[] removeAnElementWithAGivenIndex(int[] array, int index) {
      return ArrayUtils.remove(array, index);
}

Another variation is the removeAll method, which we can use to remove multiple elements from an array, given their indices:

public int[] removeAllElementsWithGivenIndices(int[] array, int... indices) {
    return ArrayUtils.removeAll(array, indices);
}

5. Using Element as Input

Or, let's say we don't know the index of what we are removing. In that case, we can provide the element to remove using ArrayUtils#removeElement:

public int[] removeFirstOccurrenceOfGivenElement(int[] array, int element) {
    return ArrayUtils.removeElement(array, element);
}

Here's another useful variation of this method ArrayUtils#removeElements, in case there is more than one element that we would like to remove:

public int[] removeAllGivenElements(int[] array, int... elements) {
    return ArrayUtils.removeElements(array, elements);
}

Sometimes, we would want to remove all occurrences of a given element. We can do so by using ArrayUtils#removeAllOccurences:

public int[] removeAllOccurrencesOfAGivenElement(int[] array, int element) {
    return ArrayUtils.removeAllOccurences(array, element);
}

6. Conclusion

In this article, we looked at the various ways of removing an element/elements from an array using the Apache Commons Lang library.

To learn more about the edge cases, please check out the source code for this tutorial and the relevant unit tests available on GitHub.

Java bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE
Comments are closed on this article!