CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-org-apache-commons--commons-lang3

Apache Commons Lang provides essential Java utility classes for string manipulation, object operations, array handling, date/time processing, reflection utilities, and more.

Pending
Overview
Eval results
Files

array-utilities.mddocs/

Array Utilities

Apache Commons Lang provides extensive array manipulation capabilities through ArrayUtils and related classes. The ArrayUtils class alone offers 368 static methods, making it a comprehensive toolkit for array operations that Java's standard library lacks.

Core Classes

ArrayUtils - Comprehensive Array Operations

The primary array utility class with 368 static methods supporting all primitive types and object arrays:

import org.apache.commons.lang3.ArrayUtils;

Constants and Empty Arrays

// Empty array constants for all types
public static final boolean[] EMPTY_BOOLEAN_ARRAY = new boolean[0]
public static final byte[] EMPTY_BYTE_ARRAY = new byte[0]
public static final char[] EMPTY_CHAR_ARRAY = new char[0]
public static final double[] EMPTY_DOUBLE_ARRAY = new double[0]
public static final float[] EMPTY_FLOAT_ARRAY = new float[0]
public static final int[] EMPTY_INT_ARRAY = new int[0]
public static final long[] EMPTY_LONG_ARRAY = new long[0]
public static final short[] EMPTY_SHORT_ARRAY = new short[0]
public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0]
public static final String[] EMPTY_STRING_ARRAY = new String[0]

// Index not found constant
public static final int INDEX_NOT_FOUND = -1

Usage Examples:

// Using empty array constants instead of creating new arrays
String[] emptyStrings = ArrayUtils.EMPTY_STRING_ARRAY;
int[] emptyInts = ArrayUtils.EMPTY_INT_ARRAY;

// Safe array initialization
String[] array = condition ? actualArray : ArrayUtils.EMPTY_STRING_ARRAY;

Array Creation and Basic Operations

// Array length and null safety
public static int getLength(Object array)
public static boolean isEmpty(Object[] array)
public static boolean isNotEmpty(Object[] array)

// Array copying and cloning
public static <T> T[] clone(T[] array)
public static <T> T[] nullToEmpty(T[] array, Class<T[]> type)

Usage Examples:

// Null-safe operations
int length = ArrayUtils.getLength(null);              // 0
boolean empty = ArrayUtils.isEmpty(new String[0]);    // true
boolean notEmpty = ArrayUtils.isNotEmpty(new String[]{"a"}); // true

// Safe array handling
String[] array = null;
String[] safe = ArrayUtils.nullToEmpty(array, String[].class); // Returns empty array

// Array cloning
String[] original = {"a", "b", "c"};
String[] copy = ArrayUtils.clone(original);

Adding Elements

// Adding single elements (returns new array)
public static boolean[] add(boolean[] array, boolean element)
public static byte[] add(byte[] array, byte element)  
public static char[] add(char[] array, char element)
public static double[] add(double[] array, double element)
public static float[] add(float[] array, float element)
public static int[] add(int[] array, int element)
public static long[] add(long[] array, long element)
public static short[] add(short[] array, short element)
public static <T> T[] add(T[] array, T element)

// Adding at specific index
public static <T> T[] add(T[] array, int index, T element)

// Adding multiple elements
public static <T> T[] addAll(T[] array1, T... array2)

Usage Examples:

// Adding single elements
int[] numbers = {1, 2, 3};
int[] moreNumbers = ArrayUtils.add(numbers, 4);           // [1, 2, 3, 4]

// Adding at specific position
String[] words = {"hello", "world"};
String[] inserted = ArrayUtils.add(words, 1, "beautiful"); // ["hello", "beautiful", "world"]

// Adding multiple elements
int[] first = {1, 2};
int[] second = {3, 4};
int[] combined = ArrayUtils.addAll(first, second);         // [1, 2, 3, 4]

// Handling null arrays safely
int[] nullArray = null;
int[] withElement = ArrayUtils.add(nullArray, 1);         // [1]

Removing Elements

// Removing elements by index
public static boolean[] remove(boolean[] array, int index)
public static <T> T[] remove(T[] array, int index)

// Removing elements by value
public static boolean[] removeElement(boolean[] array, boolean element)
public static <T> T[] removeElement(T[] array, Object element)

// Removing multiple elements
public static <T> T[] removeElements(T[] array, T... values)
public static <T> T[] removeAll(T[] array, int... indices)

// Removing all occurrences
public static <T> T[] removeAllOccurences(T[] array, T element)

Usage Examples:

// Removing by index
String[] words = {"apple", "banana", "cherry"};
String[] removed = ArrayUtils.remove(words, 1);           // ["apple", "cherry"]

// Removing by value
int[] numbers = {1, 2, 3, 2, 4};
int[] withoutTwo = ArrayUtils.removeElement(numbers, 2);  // [1, 3, 2, 4] (removes first occurrence)

// Removing multiple elements
String[] fruits = {"apple", "banana", "cherry", "date"};
String[] fewer = ArrayUtils.removeElements(fruits, "banana", "date"); // ["apple", "cherry"]

// Removing all occurrences
int[] repeated = {1, 2, 2, 3, 2, 4};
int[] cleaned = ArrayUtils.removeAllOccurences(repeated, 2); // [1, 3, 4]

Array Searching

// Basic search operations
public static int indexOf(Object[] array, Object objectToFind)
public static int indexOf(Object[] array, Object objectToFind, int startIndex)
public static int lastIndexOf(Object[] array, Object objectToFind)

// Contains operations
public static boolean contains(Object[] array, Object objectToFind)

// For primitive arrays (example with int)
public static int indexOf(int[] array, int valueToFind)
public static boolean contains(int[] array, int valueToFind)

Usage Examples:

// Searching in arrays
String[] fruits = {"apple", "banana", "cherry", "banana"};
int firstBanana = ArrayUtils.indexOf(fruits, "banana");    // 1
int lastBanana = ArrayUtils.lastIndexOf(fruits, "banana"); // 3
boolean hasApple = ArrayUtils.contains(fruits, "apple");   // true

// Searching with start index
int nextBanana = ArrayUtils.indexOf(fruits, "banana", 2);  // 3

// Primitive array search
int[] numbers = {10, 20, 30, 20, 40};
boolean hasThirty = ArrayUtils.contains(numbers, 30);      // true
int firstTwenty = ArrayUtils.indexOf(numbers, 20);        // 1

Array Manipulation

// Reversing arrays (in-place modification)
public static void reverse(Object[] array)
public static void reverse(Object[] array, int startIndexInclusive, int endIndexExclusive)

// Shuffling arrays
public static void shuffle(Object[] array)
public static void shuffle(Object[] array, Random random)

// Swapping elements
public static void swap(Object[] array, int offset1, int offset2)
public static void swap(Object[] array, int offset1, int offset2, int len)

Usage Examples:

// Array reversal
String[] words = {"first", "second", "third"};
ArrayUtils.reverse(words);                                 // ["third", "second", "first"]

// Partial reversal  
int[] numbers = {1, 2, 3, 4, 5};
ArrayUtils.reverse(numbers, 1, 4);                        // [1, 4, 3, 2, 5]

// Array shuffling
String[] deck = {"A", "B", "C", "D"};
ArrayUtils.shuffle(deck);                                 // Random order

// Element swapping
String[] items = {"a", "b", "c", "d"};
ArrayUtils.swap(items, 0, 2);                             // ["c", "b", "a", "d"]

Array Comparison and Equality

// Array comparison
public static boolean isEquals(Object array1, Object array2)
public static boolean isSameLength(Object[] array1, Object[] array2)
public static boolean isSameType(Object array1, Object array2)

// Sorted array operations
public static boolean isSorted(Comparable[] array)
public static <T> boolean isSorted(T[] array, Comparator<T> comparator)

Usage Examples:

// Array comparison
String[] array1 = {"a", "b", "c"};
String[] array2 = {"a", "b", "c"};
boolean equal = ArrayUtils.isEquals(array1, array2);      // true

// Length comparison
boolean sameLength = ArrayUtils.isSameLength(array1, array2); // true

// Sort checking
Integer[] sorted = {1, 2, 3, 4, 5};
Integer[] unsorted = {3, 1, 4, 2, 5};
boolean isSorted1 = ArrayUtils.isSorted(sorted);           // true
boolean isSorted2 = ArrayUtils.isSorted(unsorted);         // false

Subarray Operations

// Creating subarrays
public static <T> T[] subarray(T[] array, int startIndexInclusive, int endIndexExclusive)

// Array splitting
public static <T> T[][] split(T[] array, int... indices)

Usage Examples:

// Subarray extraction
String[] words = {"apple", "banana", "cherry", "date", "elderberry"};
String[] middle = ArrayUtils.subarray(words, 1, 4);       // ["banana", "cherry", "date"]

// Array splitting
int[] numbers = {1, 2, 3, 4, 5, 6, 7, 8};
int[][] parts = ArrayUtils.split(numbers, 2, 5);          // [[1,2], [3,4,5], [6,7,8]]

Type Conversion

// Primitive to Object array conversion
public static Boolean[] toObject(boolean[] array)
public static Byte[] toObject(byte[] array)
public static Character[] toObject(char[] array)
public static Double[] toObject(double[] array)
public static Float[] toObject(float[] array)
public static Integer[] toObject(int[] array)
public static Long[] toObject(long[] array)
public static Short[] toObject(short[] array)

// Object to primitive array conversion
public static boolean[] toPrimitive(Boolean[] array)
public static boolean[] toPrimitive(Boolean[] array, boolean valueForNull)
// ... similar for other types

Usage Examples:

// Converting primitives to objects
int[] primitives = {1, 2, 3, 4, 5};
Integer[] objects = ArrayUtils.toObject(primitives);       // [1, 2, 3, 4, 5]

// Converting objects to primitives
Integer[] integers = {10, 20, null, 30};
int[] converted = ArrayUtils.toPrimitive(integers, 0);     // [10, 20, 0, 30] (null becomes 0)

// Safe conversion with null handling
Boolean[] booleans = {true, false, null};
boolean[] bools = ArrayUtils.toPrimitive(booleans, false); // [true, false, false]

ArraySorter - Array Sorting Utilities

Provides convenient sorting methods for arrays:

import org.apache.commons.lang3.ArraySorter;

Sorting Operations

// Sorting primitive arrays (returns new sorted array)
public static byte[] sort(byte[] array)
public static char[] sort(char[] array)
public static double[] sort(double[] array)
public static float[] sort(float[] array)
public static int[] sort(int[] array)
public static long[] sort(long[] array)
public static short[] sort(short[] array)

// Sorting object arrays
public static <T> T[] sort(T[] array)
public static <T> T[] sort(T[] array, Comparator<? super T> comparator)

Usage Examples:

// Sorting primitive arrays
int[] unsorted = {3, 1, 4, 1, 5, 9, 2, 6};
int[] sorted = ArraySorter.sort(unsorted);                 // [1, 1, 2, 3, 4, 5, 6, 9]

// Sorting objects with custom comparator
String[] words = {"banana", "apple", "cherry"};
String[] sortedWords = ArraySorter.sort(words, String.CASE_INSENSITIVE_ORDER);

// Note: Original arrays remain unchanged
System.out.println(Arrays.equals(unsorted, sorted));      // false

ArrayFill - Array Initialization Utilities

Provides methods for filling arrays with values:

import org.apache.commons.lang3.ArrayFill;

Filling Operations

// Fill arrays with specific values
public static boolean[] fill(boolean[] a, boolean val)
public static byte[] fill(byte[] a, byte val)
public static char[] fill(char[] a, char val)
public static double[] fill(double[] a, double val)
public static float[] fill(float[] a, float val)
public static int[] fill(int[] a, int val)
public static long[] fill(long[] a, long val)
public static short[] fill(short[] a, short val)
public static <T> T[] fill(T[] a, T val)

// Fill with function-generated values
public static <T, E extends Throwable> T[] fill(T[] array, FailableIntFunction<? extends T, E> generator) throws E

Usage Examples:

// Filling arrays with values
int[] zeros = new int[5];
ArrayFill.fill(zeros, 0);                                 // [0, 0, 0, 0, 0]

String[] defaults = new String[3];
ArrayFill.fill(defaults, "default");                      // ["default", "default", "default"]

// Filling with generated values
Integer[] squares = new Integer[5];
ArrayFill.fill(squares, i -> i * i);                      // [0, 1, 4, 9, 16]

Advanced Array Operations

Multi-dimensional Array Support

// Working with multi-dimensional arrays
Object[][] matrix = {{"a", "b"}, {"c", "d"}};
int length = ArrayUtils.getLength(matrix);                 // 2
boolean empty = ArrayUtils.isEmpty(matrix[0]);             // false

// Converting jagged arrays
String[][] jagged = {{"1", "2"}, {"3"}};
String[] flattened = Arrays.stream(jagged)
    .flatMap(Arrays::stream)
    .toArray(String[]::new);                              // ["1", "2", "3"]

Performance Considerations

// Efficient array operations
public class ArrayProcessor {
    
    // Pre-allocate arrays when size is known
    public String[] processItems(List<String> items) {
        String[] result = new String[items.size()];
        for (int i = 0; i < items.size(); i++) {
            result[i] = processItem(items.get(i));
        }
        return result;
    }
    
    // Use ArrayUtils for safe operations
    public boolean[] combineFlags(boolean[] flags1, boolean[] flags2) {
        if (ArrayUtils.isEmpty(flags1)) return ArrayUtils.clone(flags2);
        if (ArrayUtils.isEmpty(flags2)) return ArrayUtils.clone(flags1);
        
        return ArrayUtils.addAll(flags1, flags2);
    }
    
    // Batch operations for better performance
    public int[] removeMultipleElements(int[] array, int... indicesToRemove) {
        // Sort indices in descending order to avoid index shifting issues
        int[] sortedIndices = Arrays.stream(indicesToRemove)
            .boxed()
            .sorted(Collections.reverseOrder())
            .mapToInt(Integer::intValue)
            .toArray();
            
        int[] result = array;
        for (int index : sortedIndices) {
            result = ArrayUtils.remove(result, index);
        }
        return result;
    }
}

Common Usage Patterns

Safe Array Processing

public class SafeArrayProcessor {
    
    public static <T> List<T> arrayToList(T[] array) {
        if (ArrayUtils.isEmpty(array)) {
            return Collections.emptyList();
        }
        return Arrays.asList(array);
    }
    
    public static String[] filterNonNull(String[] array) {
        if (ArrayUtils.isEmpty(array)) {
            return ArrayUtils.EMPTY_STRING_ARRAY;
        }
        
        return Arrays.stream(array)
            .filter(Objects::nonNull)
            .toArray(String[]::new);
    }
    
    public static <T> T getElementAt(T[] array, int index, T defaultValue) {
        if (ArrayUtils.isEmpty(array) || index < 0 || index >= array.length) {
            return defaultValue;
        }
        return array[index];
    }
}

Array Building Patterns

public class ArrayBuilder<T> {
    private List<T> elements = new ArrayList<>();
    private final Class<T> type;
    
    public ArrayBuilder(Class<T> type) {
        this.type = type;
    }
    
    public ArrayBuilder<T> add(T element) {
        if (element != null) {
            elements.add(element);
        }
        return this;
    }
    
    public ArrayBuilder<T> addAll(T... elements) {
        if (ArrayUtils.isNotEmpty(elements)) {
            Collections.addAll(this.elements, elements);
        }
        return this;
    }
    
    @SuppressWarnings("unchecked")
    public T[] build() {
        return elements.toArray((T[]) Array.newInstance(type, elements.size()));
    }
}

// Usage example
String[] result = new ArrayBuilder<>(String.class)
    .add("first")
    .addAll("second", "third")
    .add(null)  // Ignored
    .build();   // ["first", "second", "third"]

Integration with Streams

public class ArrayStreamIntegration {
    
    // Convert array to stream safely
    public static <T> Stream<T> stream(T[] array) {
        return ArrayUtils.isEmpty(array) ? 
            Stream.empty() : 
            Arrays.stream(array);
    }
    
    // Filter and collect to array
    public static String[] filterAndSort(String[] input) {
        return stream(input)
            .filter(StringUtils::isNotBlank)
            .sorted()
            .toArray(String[]::new);
    }
    
    // Group array elements
    public static Map<Boolean, List<Integer>> partitionNumbers(int[] numbers) {
        return ArrayUtils.isEmpty(numbers) ?
            Collections.emptyMap() :
            Arrays.stream(ArrayUtils.toObject(numbers))
                .collect(Collectors.partitioningBy(n -> n % 2 == 0));
    }
}

Thread Safety and Immutability

All ArrayUtils methods are thread-safe as they operate on immutable inputs and return new arrays rather than modifying existing ones:

public class ThreadSafeArrayOperations {
    
    private static final String[] IMMUTABLE_CONSTANTS = {"READ", "WRITE", "EXECUTE"};
    
    // Thread-safe array operations
    public String[] getPermissionsFor(User user) {
        String[] basePermissions = ArrayUtils.clone(IMMUTABLE_CONSTANTS);
        
        if (user.isAdmin()) {
            return ArrayUtils.add(basePermissions, "ADMIN");
        }
        
        return basePermissions;
    }
    
    // Concurrent array processing
    public int[] processConcurrently(int[] input) {
        return Arrays.stream(ArrayUtils.nullToEmpty(input, int[].class))
            .parallel()
            .map(this::expensiveOperation)
            .toArray();
    }
}

The array utilities in Apache Commons Lang provide comprehensive, null-safe, and efficient operations for all array manipulation needs, filling significant gaps in Java's standard library while maintaining excellent performance characteristics.

Install with Tessl CLI

npx tessl i tessl/maven-org-apache-commons--commons-lang3

docs

array-utilities.md

builders.md

concurrent-utilities.md

date-time-utilities.md

exception-utilities.md

index.md

math-utilities.md

object-utilities.md

string-utilities.md

validation-utilities.md

tile.json