Replace Element in Arraylist Java

The ArrayList data structure in Java allows programmers to store and manage components in a resizable array dynamically and flexibly. In this section, we will discuss various methods for replacing elements in an ArrayList in Java. Developers will be equipped with adaptable solutions through the presentation of thorough code examples and explanations for every tactic.

1. Using set() Method

One of the core operations of ArrayLists is the set() method, which lets programmers change an element at a certain index. It provides targeted replacements with efficiency and simplicity.

Syntax

Parameters

  • Index: The index at which to place the designated element.
  • E element: The element that needs to be placed at the given index.

Return Value

  • The element previously at the specified position.

Exception

  • If an index is outside of its range (index < 0 || index >= size()), an IndexOutOfBoundsException exception is thrown.

ReplaceElement.java

Output:

Modified ArrayList: [Apple, Grapes, Orange]

Explanation

The new element and the index of the element that has to be replaced are the two parameters that the set method requires. The example substitutes "Grapes" for "Banana" at index 1.

2. Setting Index Out of Bound using the set() Method

The significance of managing exceptions while trying to set an element at an index that is outside the boundaries of the ArrayList is demonstrated by this example.

Syntax

void replaceAll(UnaryOperator<E> operator)

Parameters

  • operator: UnaryOperator that will be applied to each element in the list.

Exception

  • NullPointerException: Thrown if the specified operator is null.

SetOutOfBound.java

Output:

[Apple, Banana, Cherry, Date]
IndexOutOfBoundsException: Index 6 out of bounds for length 4
[Apple, Banana, Cherry, Date]

Explanation

The example shows that when setting an element at an index that is outside of the boundaries of the ArrayList, exception handling is required. By handling the IndexOutOfBoundsException graciously, the catch block helps to avoid unexpected program termination.

3. Using replaceAll() Method

Java 8 feature lets you swap out every instance of a given element in the ArrayList.

ReplaceAll.java

Output:

Updated ArrayList: [Red, Yellow, Blue, Yellow]

Explanation

The substituteThree parameters are required for every method: the old element, the new element, and the ArrayList. The new element ("Yellow") takes the place of every instance of the old element ("Green").

4. Using Java Streams and Functional Programming

Since their introduction in Java 8, Java Streams have provided a potent and efficient means of manipulating collections, such as ArrayLists. The replaceAll() method that was previously shown makes use of functional programming ideas. More research on Java Streams and functional interfaces may yield more methods for filtering, mapping, and element manipulation.

ReplaceWithStreams.java

Output:

Modified ArrayList: [Apple, Grapes, Orange]

Explanation

In this example, every element of the original ArrayList is mapped using Java Streams. The map function conditionally switches out "Banana" with "Grapes." The outcome is gathered into a fresh ArrayList, offering a functional programming method for changing elements.

Conclusion

To sum up, this post looked at two different methods for altering elements in a Java ArrayList. The set() method provides an easy way to replace a specific element at a given index, whereas the replaceAll() function allows the replacement of all instances of a defined element.






Latest Courses