In the tutorial: “Kotlin read properties file”, We show how to Read/Write Properties from/to .Properties/.XML files by Kotlin language.
Continue reading “Kotlin read properties file from/to .Properties/.XML files”
In the tutorial: “Kotlin read properties file”, We show how to Read/Write Properties from/to .Properties/.XML files by Kotlin language.
Continue reading “Kotlin read properties file from/to .Properties/.XML files”
In the tutorial, I show how to convert Kotlin Map to List with detail steps and clearly examples.
– Use public fun Map.toList(): List
to returns a [List]
containing all key-value pairs:
val simpleMap = hashMapOf("foo" to 1, "bar" to 2)
val pairKeyValueList = simpleMap.toList()
println(pairKeyValueList) // [(bar, 2), (foo, 1)]
– Use public Collection values()
to return a view of the values:
val valueList = simpleMap.values
println(valueList) // [2, 1]
– Use public Set keySet()
to return a set view of the keys:
val keyList = simpleMap.keys
println(keyList) // [bar, foo]
– When working with Map Object, we can associate with map(transform: (T) -> R) function to customize a returned-list:
val custStores = mutableMapOf<Long, Customer>()
custStores.put(1, Customer("Jack", 20, Address("NANTERRE CT", "77471")))
custStores.put(2, Customer("Peter", 25, Address("W NORMA ST", "77009")))
val addressList = custStores.values.map{ x -> x.address }
println(addressList) // [Address(street=NANTERRE CT, postcode=77471), Address(street=W NORMA ST, postcode=77009)]
– Use MutableList
interface to modifiable list:
var mutableAddressList: MutableList<Address> = mutableListOf<Address>();
mutableAddressList.addAll(custStores.values.map{ x -> x.address })
mutableAddressList.add(Address("E NAVAHO TRL", "77449"));
println(mutableAddressList) // [Address(street=NANTERRE CT, postcode=77471), Address(street=W NORMA ST, postcode=77009), Address(street=E NAVAHO TRL, postcode=77449)]
– Create an Address model:
data class Address(
var street : String? = null,
var postcode : String? = null
){}
– Create an Customer model:
data class Customer(
var name: String? = null,
var age: Int? = null,
var address: Address = Address()) {
}
fun main(args : Array) {
//
// 1. Work with basic Map
//
val simpleMap = hashMapOf("foo" to 1, "bar" to 2)
println(simpleMap) // {bar=2, foo=1}
// 1.1 a List with pair Key-Value
val pairKeyValueList = simpleMap.toList()
println(pairKeyValueList) // [(bar, 2), (foo, 1)]
// 1.2 a List with values
val valueList = simpleMap.values
println(valueList) // [2, 1]
// 1.3 a List with keys
val keyList = simpleMap.keys
println(keyList) // [bar, foo]
//
// 2. Work with Object Map
//
val custStores = mutableMapOf()
custStores.put(1, Customer("Jack", 20, Address("NANTERRE CT", "77471")))
custStores.put(2, Customer("Peter", 25, Address("W NORMA ST", "77009")))
println(custStores); // {1=Customer(name=Jack, age=20, address=Address(street=NANTERRE CT, postcode=77471)), 2=Customer(name=Peter, age=25, address=Address(street=W NORMA ST, postcode=77009))}
// 2.1 a List with pair Key-Value Objects
val pairKeyValueCustList = custStores.toList();
println(pairKeyValueCustList) // [(1, Customer(name=Jack, age=20, address=Address(street=NANTERRE CT, postcode=77471))), (2, Customer(name=Peter, age=25, address=Address(street=W NORMA ST, postcode=77009)))]
// 2.2 a List with Object Values
val customersList = custStores.values
println(customersList) // [Customer(name=Jack, age=20, address=Address(street=NANTERRE CT, postcode=77471)), Customer(name=Peter, age=25, address=Address(street=W NORMA ST, postcode=77009))]
// 2.3 Customize Objects with map function: (Customer -> Address)
val addressList = custStores.values.map{ x -> x.address }
println(addressList) // [Address(street=NANTERRE CT, postcode=77471), Address(street=W NORMA ST, postcode=77009)]
// 2.4 Using MutableList interface for a modifiable List
var mutableAddressList: MutableList = mutableListOf();
mutableAddressList.addAll(custStores.values.map{ x -> x.address })
mutableAddressList.add(Address("E NAVAHO TRL", "77449"));
println(mutableAddressList) // [Address(street=NANTERRE CT, postcode=77471), Address(street=W NORMA ST, postcode=77009), Address(street=E NAVAHO TRL, postcode=77449)]
}
– Reference: Kotlin Collection Transformations
– Related posts:
Tutorial “foreachindexed Kotlin Example”
In the tutorial, I will show you how to use Kotlin forEachIndexed
method to loop through Kotlin Array, List, Map collections.
forEachIndexed
method performs the given action on each element, providing sequential index with the element.
inline fun <T> Array<out T>.forEachIndexed(
action: (index: Int, T) -> Unit)
inline fun ByteArray.forEachIndexed(
action: (index: Int, Byte) -> Unit)
inline fun ShortArray.forEachIndexed(
action: (index: Int, Short) -> Unit)
inline fun IntArray.forEachIndexed(
action: (index: Int, Int) -> Unit)
inline fun LongArray.forEachIndexed(
action: (index: Int, Long) -> Unit)
inline fun FloatArray.forEachIndexed(
action: (index: Int, Float) -> Unit)
inline fun DoubleArray.forEachIndexed(
action: (index: Int, Double) -> Unit)
inline fun BooleanArray.forEachIndexed(
action: (index: Int, Boolean) -> Unit)
inline fun CharArray.forEachIndexed(
action: (index: Int, Char) -> Unit)
inline fun <T> Iterable<T>.forEachIndexed(
action: (index: Int, T) -> Unit)
@ExperimentalUnsignedTypes inline fun UIntArray.forEachIndexed(
action: (index: Int, UInt) -> Unit)
@ExperimentalUnsignedTypes inline fun ULongArray.forEachIndexed(
action: (index: Int, ULong) -> Unit)
@ExperimentalUnsignedTypes inline fun UByteArray.forEachIndexed(
action: (index: Int, UByte) -> Unit)
@ExperimentalUnsignedTypes inline fun UShortArray.forEachIndexed(
action: (index: Int, UShort) -> Unit)
Performs the given action on each element, providing sequential index with the element.
Parameters: action
– function that takes the index of an element and the element itself and performs the action on the element.
Method Signature:
public inline fun <T> Array<out T>.forEachIndexed(action: (index: Int, T) -> Unit): Unit
Practice – foreachindexed Example 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)
*/
Method signature:
public inline fun <T> Iterable<T>.forEachIndexed(action: (index: Int, T) -> Unit): Unit
Practice – foreachindexed Kotlin Example 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)
*/
Here is the full sourcecode with the foreachindexed
kotlin method. We create 2 data-structure: Kotlin Array and Kotlin List, then apply the foreachindexed
method with the 2 collections in a main function.
package com.loizenjava.kotlinforeach
data class Customer(
val name: String,
val age: Int
)
fun main(args : Array<String>) {
val simpleArray = arrayOf(1, 2, 3, 4)
val customerArray = arrayOf(Customer("Craig", 45),
Customer("Amos", 23),
Customer("Jack", 20))
// 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)
*/
}
Reference: foreachindexed Kotlin Method
Related Tutorial: