Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE

1. Introduction

Arrays are fundamental data structures that provide a way to store multiple elements of the same type in a contiguous block of memory. However, they have certain limitations, such as a fixed size and a lack of convenient methods for manipulation and traversal.

In this tutorial, we’ll delve into the process of converting Java arrays into Iterable data structures and unlock a wide range of built-in functionalities that can greatly simplify and enhance data manipulation.

Converting Java arrays to Iterable structures is essential to gain advanced functionalities, improve code readability, maintainability, and reusability.

2. Using Stream API collect()

Stream API offers the most convenient way to convert an int[] array to an Iterable in Java. The IntStream interface provides a set of methods specifically designed for working with streams of primitive integers. Let’s use it for our goal:

int[] array = new int[] {1, 2, 3};
List<Integer> integers = Arrays.stream(array)
  .boxed()
  .collect(Collectors.toList())

In the code above, we create an IntStream with Arrays.stream(), wrap each primitive int element to Integer with boxed(), and finally, pack it to List<Integer> with collect(). This List can be easily used as an Iterable. It allows us to iterate over its elements using enhanced for-loops or any other method compatible with Iterables.

3. Using Stream API iterator()

Another approach to convert an int[] array to an Iterable is using the Stream API’s iterator() method. It allows us to obtain an iterator directly from a stream, offering another convenient option for the conversion process. By combining it with a lambda expression or an anonymous class, we can create our own implementation of an Iterable.

Here’s an example of using a lambda expression to create an Iterable<Integer>:

Iterable<Integer> convertWithStreamIterator(int[] array) {
    return () -> Arrays.stream(array).iterator();
}

Alternatively, we can achieve the same result using an anonymous class:

Iterable<Integer> convertWithStreamIterator(int[] array) {
    return new Iterable<Integer>() {
        @Override
        public Iterator<Integer> iterator() {
            return Arrays.stream(array).iterator();
        }
    };
}

4. Using Apache Commons Lang ArrayUtils

Apache Commons Lang library provides a rich set of utility classes that simplify common programming tasks.

Let’s add the Maven dependency in our pom.xml file:

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

Now we can use ArrayUtils in code:

int [] array = new int [] {1, 2, 3};
List<Integer> integers = Arrays.asList(ArrayUtils.toObject(array));

Here, we use the ArrayUtils.toObject() method to convert an int[] array into an Integer[] array. Then, we pass the Integer[] array to the Arrays.asList() method and obtain a List<Integer>, which is an Iterable collection.

5. Using Guava

Google’s Guava library offers many helpful utilities for Java developers. The Ints.asList() method can be used to convert an int[] array to a List<Integer>, which serves as an Iterable.

Let’s add the Maven dependency in our pom.xml file:

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>32.0.1-jre</version>
</dependency>

Now we can use Ints.asList() in code:

int[] array = {1, 2, 3};
List<Integer> integers = Ints.asList(array);

6. Conclusion

In this article, we explored several approaches to convert a Java array to an Iterable collection. We discussed the convenience and flexibility offered by each method, allowing developers to choose the approach that best suits their needs.

The Stream API offers collect() and iterator() methods, using streams to convert the array into an Iterable. Apache ArrayUtils in Apache Commons Lang converts int[] to Integer[] and then to List<Integer>. Guava’s Ints.asList() directly converts the array to List<Integer>.

By exploring and understanding these various approaches, we can confidently select the most appropriate method that aligns with our coding style, project requirements, and overall objectives.

As always, the source code for the examples is available over on GitHub.

Course – LS – All

Get started with Spring and Spring Boot, through the Learn Spring course:

>> CHECK OUT THE COURSE
res – REST with Spring (eBook) (everywhere)
Comments are closed on this article!