Convert Java Array to Iterable
To convert a array to an Iterable
, you may use Arrays.asList()
:
And here is iteration in action:
Keep in mind that any changes to iterable
reflect in nums
. For a modifiable Iterable
, do:
But when we talk about primitive arrays, such as int[]
, Arrays.asList()
won't come to our rescue. We need to scout out different methods, as detailed below.
Handling primitive arrays: Step up, Java 8 streams!
To convert int[]
to Iterable<Integer>
, Java 8 Streams swoops in to save the day:
In the above, IntStream.of(primitiveNums).boxed()
autoboxes our int
to Integer
, making it compatible with Iterable<Integer>
.
Dodging the Array.asList() bullet
Ye Olde Rule: Do not use Arrays.asList()
directly on primitives. Here's why:
External libraries to the rescue
Fret not, Guava's Ints.asList()
is here to aid:
Guava handles the autoboxing internally. Thus, providing a clean, efficient solution without racking your brains on streams or crafting custom implementations.
Go Custom, when there's no other way
At times, you might want to roll up your coding sleeves and create a custom Iterable
for your array:
Apply it like:
When arrays meet functional programming
The Iterable functional interface
Java 8's functional interfaces meet lambda expressions to make everything more concise:
This lambda expression exploits the Iterable functional interface, providing a custom iterator()
method.
Streams: Breaking the stereotype of functional programming
Java 8 streams stand for a functional approach to processing collections. They make conversions, especially from primitive to boxed types, tasty!
Elegant code FTW
Streams and lambdas push you to write expressive, readable, and succinct codes. Say hello to modern Java development practices.
Don't Forget!
Efficiency isn't just a Word, but Practice
While whipping up solutions, efficiency should be in your ingredient list. Avoid bloating codebase or degrading performance with unnecessary operations.
UnsupportedOperationException is NOT your BFF
Handle UnsupportedOperationException
while converting to modifiable collections. Not all Iterables
play nice with the remove()
operation.
Clear Explanations: A Must-Have
When dealing with solutions like Guava or Java 8 features, it's important to give clarity a front seat. Only when they're well-explained, they're well-received.
Was this article helpful?