CollectionUtils reverseArray() Example in Java

In this post we will see CollectionUtils reverseArray() example in Java. we will see how to use CollectionUtils reverseArray() method

We need to add the below dependency in maven to use org.apache.commons.lang3.CollectionUtils reverseArray() method. We can download the apache-commons maven dependency from here.

pom.xml changes

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-collections4</artifactId>
<version>4.4</version>
</dependency>

CollectionUtils reverseArray() Example in Java

package com.javatute;

import org.apache.commons.collections4.CollectionUtils;

import java.util.Arrays;

public class CollectionUtilsReverseArrayExample {
    public static void main(String[] args) {

        //create a array of String type
        String[] strArrays = new String[4];
        //add some element to strArray
        strArrays[0] = "Sansa";
        strArrays[1] = "James";
        strArrays[2] = "Jack";
        strArrays[3] = "Josef";
        System.out.println(Arrays.stream(strArrays).toList());
        CollectionUtils.reverseArray(strArrays);
        System.out.println("----After CollectionUtils.reverseArray()----");
        System.out.println(Arrays.stream(strArrays).toList());
    }
}

Output is

[Sansa, James, Jack, Josef]
—-After CollectionUtils.reverseArray()—-
[Josef, Jack, James, Sansa]

Let’s see a few questions related to CollectionUtils reverseArray().

How do you reverse an array in Collections in Java?

We can use CollectionUtils.reverseArray() method.

That’s all about CollectionUtils reverseArray() Example in Java.

Related post