The java.util.Arrays class provides a collection of static methods to play with arrays, such as sorting, searching, and converting arrays to collections and vice versa.

Key Points:

Utility Methods: Offers methods for manipulating arrays.

Algorithms: Contains sorting, searching, and comparison algorithms tailored for arrays.

Conversion: Can convert between lists and arrays.

Parallel Operations: Java 8 introduced parallel versions of some operations for better performance with large arrays.

Java.util.Arrays Common 10 Utility Methods Example

import java.util.Arrays;

public class ArraysUtilityDemo {

    public static void main(String[] args) {

        // 1. Convert an array to string
        int[] numbers = {3, 5, 1, 4, 2};
        System.out.println("1. Convert to string: " + Arrays.toString(numbers));

        // 2. Sort the array
        Arrays.sort(numbers);
        System.out.println("2. Sorted array: " + Arrays.toString(numbers));

        // 3. Search for a number using binary search
        int index = Arrays.binarySearch(numbers, 4);
        System.out.println("3. Index of number 4: " + index);

        // 4. Check if two arrays are equal
        int[] anotherArray = {1, 2, 3, 4, 5};
        System.out.println("4. Arrays equal? " + Arrays.equals(numbers, anotherArray));

        // 5. Fill the array with a specific value
        Arrays.fill(numbers, 8);
        System.out.println("5. Array after fill method: " + Arrays.toString(numbers));

        // 6. Copy an array
        int[] copiedArray = Arrays.copyOf(anotherArray, anotherArray.length);
        System.out.println("6. Copied array: " + Arrays.toString(copiedArray));

        // 7. Copy a specific range of an array
        int[] rangeCopy = Arrays.copyOfRange(anotherArray, 2, 4);
        System.out.println("7. Range copied (2 to 4): " + Arrays.toString(rangeCopy));

        // 8. Compare two arrays lexicographically
        int compareResult = Arrays.compare(numbers, anotherArray);
        System.out.println("8. Comparison result: " + compareResult);

        // 9. Create a list from an array
        System.out.println("9. List from array: " + Arrays.asList("A", "B", "C"));

        // 10. Create a deep string representation for multi-dimensional arrays
        int[][] multiArray = {{1, 2}, {3, 4}};
        System.out.println("10. Deep string of multi-dimensional array: " + Arrays.deepToString(multiArray));
    }
}

Output:

1. Convert to string: [3, 5, 1, 4, 2]
2. Sorted array: [1, 2, 3, 4, 5]
3. Index of number 4: 3
4. Arrays equal? true
5. Array after fill method: [8, 8, 8, 8, 8]
6. Copied array: [1, 2, 3, 4, 5]
7. Range copied (2 to 4): [3, 4]
8. Comparison result: -1
9. List from array: [A, B, C]
10. Deep string of multi-dimensional array: [[1, 2], [3, 4]]

Explanation:

1. Used Arrays.toString() to get a string representation of the array.

2. The Arrays.sort() method sorts the array elements in ascending order.

3. The Arrays.binarySearch() method finds the index of a specific element in a sorted array.

4. The Arrays.equals() method checks if two arrays have the same content.

5. The Arrays.fill() method assigns a specific value to each element in the array.

6. Arrays.copyOf() creates a copy of the source array.

7. Arrays.copyOfRange() copies a specific range of elements from the source array.

8. The Arrays.compare() method lexicographically compares two arrays.

9. The Arrays.asList() method creates a fixed-size list backed by the specified array.

10. For multi-dimensional arrays, Arrays.deepToString() provides a deep string representation.

Java.util.Arrays Parallel Operations Example

import java.util.Arrays;

public class ArraysParallelDemo {

    public static void main(String[] args) {

        // 1. Create an array of integers
        int[] numbers = {3, 5, 1, 4, 2, 6, 7, 9, 8};

        // 2. Sort the array using parallelSort
        Arrays.parallelSort(numbers);
        System.out.println("2. Parallel sorted array: " + Arrays.toString(numbers));

        // 3. Perform a parallel set operation
        Arrays.parallelSetAll(numbers, index -> index + 10);
        System.out.println("3. Array after parallelSetAll: " + Arrays.toString(numbers));

        // 4. Perform a parallel prefix operation
        Arrays.parallelPrefix(numbers, (left, right) -> left + right);
        System.out.println("4. Array after parallelPrefix: " + Arrays.toString(numbers));
    }
}

Output:

2. Parallel sorted array: [1, 2, 3, 4, 5, 6, 7, 8, 9]
3. Array after parallelSetAll: [10, 11, 12, 13, 14, 15, 16, 17, 18]
4. Array after parallelPrefix: [10, 21, 33, 46, 60, 75, 91, 108, 126]

Explanation:

1. Initialized an array of integers to demonstrate parallel operations.

2. Arrays.parallelSort() is a parallel sorting algorithm which takes advantage of multiple processors. It breaks the array into sub-arrays and sorts them in parallel. Then, it merges them together.

3. Arrays.parallelSetAll() assigns values to the array elements in parallel, based on the provided function. Here, each element is set to its index plus 10.

4. Arrays.parallelPrefix() updates each element to be the sum of itself and all preceding elements in the array using a parallel algorithm.

Java.util.Arrays Conversion Examples – Convert Array to List and Vice Versa

import java.util.Arrays;
import java.util.List;

public class ArraysConversionDemo {

    public static void main(String[] args) {

        // 1. Convert an array to a List
        String[] fruitArray = {"Apple", "Banana", "Cherry"};
        List<String> fruitList = Arrays.asList(fruitArray);
        System.out.println("1. Converted List from array: " + fruitList);

        // 2. Convert a List to an array
        List<String> veggiesList = Arrays.asList("Carrot", "Broccoli", "Peas");
        String[] veggiesArray = veggiesList.toArray(new String[0]);
        System.out.println("2. Converted array from List: " + Arrays.toString(veggiesArray));

    }
}

Output:

1. Converted List from array: [Apple, Banana, Cherry]
2. Converted array from List: [Carrot, Broccoli, Peas]

Explanation:

1. Arrays.asList() is a handy method for converting an array to a fixed-size list backed by the original array. Any changes to the list are reflected in the array and vice-versa.

2. List.toArray() is a method of the List interface that converts a list back to an array. We can pass an array (or an empty array of the required type) to this method to determine the runtime type of the array returned.