Kotlin Fold Example

This Kotlin tutorial shows you example that uses Fold method on List, Map of Objects.


>>> Refer to: JavaSampleApproach.com

I. Technology

– Java 1.8
– Kotlin 1.1.2

II. Overview

1. fold()

We will get total quantity for of Product(name,quantity) List/Map using fold() method:

This function helps to accumulate value starting with initial value, then apply operation from left to right to current accumulator value and each element.

2. foldRight()

If you want to apply operation from right to left, you can use foldRight():

III. Practice

0. Product Class

1. List

Result:

2. Map

Result:

Add Comment