robin
robin

Reputation: 1925

Kotlin Stream usage

I have a code like below

items.forEach { item ->
            request += getDetails(item.propertyId, item.destinationIds)
            count++
            if( count == bulkSize) {
                save(request)
                request = ""
                count = 0
            }
        }

        if(!request.isEmpty()) {
            save(request)
        }

How can I use streaming api to make the code less verbose ?

Upvotes: 0

Views: 131

Answers (1)

IlyaMuravjov
IlyaMuravjov

Reputation: 2492

You can do it like this:

items.chunked(bulkSize) { chunk ->
    save(chunk.joinToString(separator = "") { item ->
        getDetails(item.propertyId, item.destinationIds) 
    })
}

Upvotes: 2

Related Questions