Kotlin forEach & forEachIndexed method on Array, List, Map collection example

kotlin-foreach-foreachIndexed-method-on-array-list-map-collection-example-feature-image

In the tutorial, Grokonez will show you how to use Kotlin forEach and forEachIndexed methods to loop through Kotlin Array, List, Map collections.

I. forEach method

forEach method is used to performs the given action on each element.

1. with Array

Method signature:

public inline fun  Array.forEach(action: (T) -> Unit): Unit

Practice:

val simpleArray = arrayOf(1, 2, 3,  4)
simpleArray.forEach { println(it) }
// print on console
// ->
/*
	1
	2
	3
	4		
*/

val customerArray = arrayOf(Customer("Craig", 45),
								Customer("Amos", 23),
								Customer("Jack", 20))

customerArray.forEach { println(it) }
// println on console
// ->
/*
	Customer(name=Craig, age=45)
	Customer(name=Amos, age=23)
	Customer(name=Jack, age=20) 
*/
2. with List

Method signature:

public inline fun  Iterable.forEach(action: (T) -> Unit): Unit

Practice:

val simpleList = listOf(5, 6, 7, 8)
simpleList.forEach{println(it)}
// print on console
// ->
/*
	5
	6
	7
	8
*/

val customerList = listOf(Customer("Smith", 26),
								Customer("Peter", 43),
								Customer("Mary", 27))
customerList.forEach { println(it) }
// print on console
// ->
/*
	Customer(name=Smith, age=26)
	Customer(name=Peter, age=43)
	Customer(name=Mary, age=27) 
*/
3. with Map

Method Signature:

public inline fun  Map.forEach(action: (Map.Entry) -> Unit): Unit

Practice:

val simpleHashMap = hashMapOf(1 to "one", 2 to "two", 3 to "three" )
simpleHashMap.forEach{ println(it) }
// println on console
// ->
/*
	1=one
	2=two
	3=three
*/

val customerHashMap = hashMapOf("Smith" to Customer("Smith", 26),
								"Peter" to Customer("Peter", 43),
								"Mary" to Customer("Mary", 27))
customerHashMap.forEach{ println(it) }
// println on console
// ->
/*
	Smith=Customer(name=Smith, age=26)
	Peter=Customer(name=Peter, age=43)
	Mary=Customer(name=Mary, age=27)
*/

II. forEachIndexed method

forEachIndexed method performs the given action on each element, providing sequential index with the element.

1. with Array

Method Signature:

public inline fun  Array.forEachIndexed(action: (index: Int, T) -> Unit): Unit

Practice:

simpleArray.forEachIndexed{index, element -> println("index = $index, element = $element")}
// print on console
// ->
/*
	index = 0, element = 1
	index = 1, element = 2
	index = 2, element = 3
	index = 3, element = 4 
*/
customerArray.forEachIndexed{index, customer -> println("index = $index, customer = $customer")}
// print on console
// ->
/*
	index = 0, customer = Customer(name=Craig, age=45)
	index = 1, customer = Customer(name=Amos, age=23)
	index = 2, customer = Customer(name=Jack, age=20)
*/
2. with List

Method signature:

public inline fun  Iterable.forEachIndexed(action: (index: Int, T) -> Unit): Unit

Practice:

simpleList.forEachIndexed{index, element -> println("index = $index, element = $element")}
// print on console
// ->
/*
	index = 0, element = 5
	index = 1, element = 6
	index = 2, element = 7
	index = 3, element = 8
*/

customerList.forEachIndexed{index, customer -> println("index = $index, customer = $customer")}
// print on console
// ->
/*
	index = 0, customer = Customer(name=Smith, age=26)
	index = 1, customer = Customer(name=Peter, age=43)
	index = 2, customer = Customer(name=Mary, age=27)
*/

III. Full sourcecode

package com.javasampleapproach.kotlinforeach

data class Customer(
	val name: String,
	val age: Int
)

fun main(args : Array) {
	
	println("#############################################################")
	println("---------------------I. forEach() method---------------------")
	println("#############################################################")
	
	println("-------------1. work with Array-------------")
	val simpleArray = arrayOf(1, 2, 3,  4)
	simpleArray.forEach { println(it) }
	// print on console
	// ->
	/*
		1
		2
		3
		4		
 	*/
	
	val customerArray = arrayOf(Customer("Craig", 45),
									Customer("Amos", 23),
									Customer("Jack", 20))
	
	customerArray.forEach { println(it) }
	// println on console
	// ->
	/*
		Customer(name=Craig, age=45)
		Customer(name=Amos, age=23)
		Customer(name=Jack, age=20) 
 	*/
	
	println("-------------2. work with List-------------")
	val simpleList = listOf(5, 6, 7, 8)
	simpleList.forEach{println(it)}
	// print on console
	// ->
	/*
		5
		6
		7
		8
 	*/
	
	val customerList = listOf(Customer("Smith", 26),
									Customer("Peter", 43),
									Customer("Mary", 27))
	customerList.forEach { println(it) }
	// print on console
	// ->
	/*
		Customer(name=Smith, age=26)
		Customer(name=Peter, age=43)
		Customer(name=Mary, age=27) 
 	*/
	
	println("-------------3. work with HashMap -------------")
	val simpleHashMap = hashMapOf(1 to "one", 2 to "two", 3 to "three" )
	simpleHashMap.forEach{ println(it) }
	// println on console
	// ->
	/*
		1=one
		2=two
		3=three
 	*/
	
	val customerHashMap = hashMapOf("Smith" to Customer("Smith", 26),
									"Peter" to Customer("Peter", 43),
									"Mary" to Customer("Mary", 27))
	customerHashMap.forEach{ println(it) }
	// println on console
	// ->
	/*
		Smith=Customer(name=Smith, age=26)
		Peter=Customer(name=Peter, age=43)
		Mary=Customer(name=Mary, age=27)
 	*/
	
	// II. forEachIndexed() method
	println("#############################################################")
	println("-----------------II. forEachIndexed() method-----------------")
	println("#############################################################")
	// 1. work with Array
	println("-------------1. work with Array-------------")
	simpleArray.forEachIndexed{index, element -> println("index = $index, element = $element")}
	// print on console
	// ->
	/*
		index = 0, element = 1
		index = 1, element = 2
		index = 2, element = 3
		index = 3, element = 4 
 	*/
	customerArray.forEachIndexed{index, customer -> println("index = $index, customer = $customer")}
	// print on console
	// ->
	/*
		index = 0, customer = Customer(name=Craig, age=45)
		index = 1, customer = Customer(name=Amos, age=23)
		index = 2, customer = Customer(name=Jack, age=20)
 	*/
	
	// 2. work with List
	println("-------------2. work with List-------------")
	simpleList.forEachIndexed{index, element -> println("index = $index, element = $element")}
	// print on console
	// ->
	/*
		index = 0, element = 5
		index = 1, element = 6
		index = 2, element = 7
		index = 3, element = 8
 	*/
	
	customerList.forEachIndexed{index, customer -> println("index = $index, customer = $customer")}
	// print on console
	// ->
	/*
		index = 0, customer = Customer(name=Smith, age=26)
		index = 1, customer = Customer(name=Peter, age=43)
		index = 2, customer = Customer(name=Mary, age=27)
 	*/
}

6 thoughts on “Kotlin forEach & forEachIndexed method on Array, List, Map collection example”

  1. Do you have a spam problem on this website; I also am a blogger,
    and I was wondering your situation; we have developed some
    nice methods and we are looking to exchange strategies with others,
    why not shoot me an e-mail if interested.

  2. Hmm it appears like your website ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well am an aspiring blog blogger but I’m still new to everything. Do you have any tips and hints for rookie blog writers? I’d definitely appreciate it.

  3. You really make it appear so easy with your presentation however I to find this matter to be actually one thing which I feel I would never understand. It sort of feels too complicated and extremely large for me. I am taking a look forward in your next put up, I抣l attempt to get the hang of it!

  4. Thanks for this excellent article. Also a thing is that almost all digital cameras are available equipped with the zoom lens that allows more or less of a scene to generally be included simply by ‘zooming’ in and out. Most of these changes in target length are generally reflected from the viewfinder and on huge display screen at the back of your camera.

Leave a Reply

Your email address will not be published. Required fields are marked *