Java – Iterate over Array indexes using IntStream

Streams of elements usually do not allow access to the index value of the current item. To iterate over an array or ArrayList while having access to indexes, use IntStream.range(start, endExclusive).




public class IntStreamToIterateOverIndexes {

	public static void main(String[] args) {
		String[] fruitList = { "Apple", "Banana", "Orange", "Kiwi",
				"Grape" };
		IntStream.range(0, fruitList.length).mapToObj(
				i -> String.format("%d) %s", i + 1, fruitList[i]))



1) Apple
2) Banana
3) Orange
4) Kiwi
5) Grape

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s