Convert an Array to Iterable in Java
This post will discuss how to convert an array to Iterable in Java.
1. Using Java 8 and above
In Java 8 and above, we can use lambda to convert the iterator to iterable , as shown below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
import java.util.Iterator; // Program to convert an array to Iterable in Java 8 and above class Main { public static void main(String[] args) { int[] arr = { 1, 2, 3, 4, 5 }; // After Java 8 Iterable<Integer> iterable = () -> new Iterator<Integer>() { private int index = 0; @Override public boolean hasNext() { return arr.length > index; } @Override public Integer next() { return arr[index++]; } }; iterable.forEach(System.out::println); } } |
Output:
1
2
3
4
5
2. Using Java 7
With Java 7 and before, we can do something like:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
import java.util.Iterator; // Program to convert an array to Iterable class Main { public static void main(String[] args) { int[] arr = { 1, 2, 3, 4, 5 }; // Before Java 7 Iterable<Integer> iterable = new Iterable<Integer>() { @Override public Iterator iterator() { return new Iterator() { private int index = 0; @Override public boolean hasNext() { return arr.length > index; } @Override public Object next() { return arr[index++]; } }; } }; iterable.forEach(System.out::println); } } |
Output:
1
2
3
4
5
3. Using Guava Library
Guava’s com.google.common.primitives package provides utility classes for all primitives types such as Ints, Doubles, Longs, Chars, etc. To get an Iterable, we can wrap the specified primitive array as a list of the corresponding wrapper type using the asList() method provided by each primitive class.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import com.google.common.primitives.Ints; // Program to convert an array to Iterable using Guava class Main { public static void main(String[] args) { int[] arr = { 1, 2, 3, 4, 5 }; // Using Guava Iterable<Integer> iterable = Ints.asList(arr); iterable.forEach(System.out::println); } } |
Output:
1
2
3
4
5
For Wrapper classes or arrays with non-primitive types, we can use the Iterators.forArray() method to get an iterator, and we can easily get an iterable from the iterator using lambda as already seen before.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import com.google.common.collect.Iterators; // Program to convert a non-primitive array to Iterable in Java 8 and above using Guava class Main { public static void main(String[] args) { Integer[] arr = { 1, 2, 3, 4, 5 }; // Using Guava Iterable<Integer> iterable = () -> Iterators.forArray(arr); iterable.forEach(System.out::println); } } |
Output:
1
2
3
4
5
4. Using Arrays.stream() method
The idea is to get an Iterator by calling the iterator() method on IntStream, DoubleStream or LongStream. Then we get an iterable from the iterator using lambda.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.util.Arrays; // Program to convert an array to Iterable in Java 8 and above class Main { public static void main(String[] args) { int[] arr = { 1, 2, 3, 4, 5 }; // 1. Using `boxed()` for converting `IntStream` to `Stream<Integer> Iterable<Integer> iterable1 = () -> Arrays.stream(arr) .boxed() .iterator(); // 2. Using `mapToObj()` for converting `IntStream` to `Stream<Integer> Iterable<Integer> iterable2 = () -> Arrays.stream(arr) .mapToObj(Integer::valueOf) .iterator(); // 3. Implicit conversion from `IntStream` to `Stream<Integer> Iterable<Integer> iterable3 = () -> Arrays.stream(arr) .iterator(); iterable1.forEach(System.out::println); } } |
Output:
1
2
3
4
5
5. Using of() method
We can also use IntStream.of(), DoubleStream.of() or LongStream.of() in place of overloaded Arrays.stream() for int, double and long values, respectively.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
import java.util.stream.IntStream; // Program to convert an array to Iterable in Java 8 and above class Main { public static void main(String[] args) { int[] arr = { 1, 2, 3, 4, 5 }; // 1. Using `boxed()` for converting `IntStream` to `Stream<Integer>` Iterable<Integer> iterable1 = () -> IntStream.of(arr) .boxed() .iterator(); // 2. Using `mapToObj()` for converting `IntStream` to `Stream<Integer>` Iterable<Integer> iterable2 = () -> IntStream.of(arr) .mapToObj(Integer::valueOf) .iterator(); // 3. Implicit conversion from `IntStream` to `Stream<Integer>` Iterable<Integer> iterable3 = () -> IntStream.of(arr) .iterator(); iterable1.forEach(System.out::println); } } |
Output:
1
2
3
4
5
For Non-Primitive Arrays
A list is an iterable in Java since the List interface extends the java.util.Collection interface, which happens to extend the java.lang.Iterable interface. So for non-primitive arrays, we can simply use Arrays.asList() to get iterable over the array.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import java.util.Arrays; // Program to convert a non-primitive array to Iterable class Main { public static void main(String[] args) { Integer[] arr = { 1, 2, 3, 4, 5 }; Iterable<Integer> iterable = Arrays.asList(arr); iterable.forEach(System.out::println); } } |
Output:
1
2
3
4
5
That’s all about converting an array to Iterable in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)