DEV Community

loizenai
loizenai

Posted on

Kotlin Partition method – split List, Map of Objects example

Kotlin Partition method – split List, Map of Objects example

https://grokonez.com/kotlin/kotlin-partition-method-split-list-map-objects-example

This Kotlin tutorial shows you example that uses Partition method to split List, Map of Objects.

I. Technology

  • Java 1.8
  • Kotlin 1.1.2

    II. Overview

  • We will split List/Map of Product(name,quantity) objects into 2 Lists using partition() method:
    
    inline fun  Iterable.partition(
    predicate: (T) -> Boolean
    ): Pair, List>
    
    predicate is the condition for the first List of Pair, all remain items will be contained in the second List.
  • We also use Destructuring Declaration syntax to destructure the Pair into 2 Lists.
    
    val (positive, negative) = List.partition { predicate }
    

    III. Practice

    0. Product Class

https://grokonez.com/kotlin/kotlin-partition-method-split-list-map-objects-example

Top comments (0)