Mutable list with different types of data

aswi

I have a mutable list which can have items of different data classes. For example, val items: List<ItemData>, and ItemData is a class of

sealed class ItemData {
    data class itemOne(
            val valueOne
    ) : ItemData()

    data class itemTwo(
            val valueTwo
    ) : ItemData()

    data class itemThree(
            val valueThree
    ) : ItemData()
}

Say I want to take the list and whenever item is of certain type, update some value there. My question is, how to make sure that item is exactly the type I need? I tried with item as ItemData.itemTwo but I get class cast exception, since in my list are items of other types, too.

theThapa

@aswi We can achieve this by using when with is check, please refer to below code sample:

// ItemData class
sealed class ItemData {
    data class ItemOne(
        val valueOne: Int
    ) : ItemData()

    data class ItemTwo(
        val valueTwo: String
    ) : ItemData()

    data class ItemThree(
        val valueThree: Boolean
    ) : ItemData()
}

This is how the type check works:

@JvmStatic
fun main(args: Array<String>) {

    val mixedList = listOf(
        ItemData.ItemOne(1), ItemData.ItemTwo("One"), ItemData.ItemThree(true)
    )
    mixedList.forEach { itemData ->
        when (itemData) {
             is ItemData.ItemOne -> println("This is ItemOne")
             is ItemData.ItemTwo -> println("This is ItemTwo")
             is ItemData.ItemThree -> println("This is ItemThree")
        }
    }
 }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Django admin list_display of different data types with null

From Dev

Constructor and different data types are confusing

From Dev

Building list of items of different types in database

From Dev

Exclude items of one list in another with different object data types, LINQ?

From Dev

List of pointers to different types of objects

From Dev

How to join list with different types of elements in python?

From Dev

How to list and count the different types of node and edge entities in the graph data using SPARQL query?

From Dev

List of template classes of different types

From Dev

Share data with different types in UIActivityViewController

From Dev

Python - "Joining" list of lists of different types

From Dev

How to parse xml with simple-xml library ,where data is list and consist of different types of list?

From Dev

Filter in List with custom data types

From Dev

Sorting objects of different types into one list

From Dev

Folding a list of different types using Shapeless in Scala

From Dev

How could a List contains different types in java?

From Dev

Storing a list of different generic types in a class

From Dev

A more pythonic (or pandorable) way to change a list of columns to different data types

From Dev

Mutable list with different types of data

From Dev

Private propertys and mutable types

From Dev

Different Data types Order By in SQL

From Dev

How to list and count the different types of node and edge entities in the graph data using SPARQL query?

From Dev

Object data with 2 different types

From Dev

Operations on different data types

From Dev

How to parse xml with simple-xml library ,where data is list and consist of different types of list?

From Dev

Filter in List with custom data types

From Dev

A more pythonic (or pandorable) way to change a list of columns to different data types

From Dev

TreeTableView: Displaying different data types

From Dev

Sort a list of lists in Python with different data types

From Dev

Put Different Data Types in a Dynamic List

Related Related

  1. 1

    Django admin list_display of different data types with null

  2. 2

    Constructor and different data types are confusing

  3. 3

    Building list of items of different types in database

  4. 4

    Exclude items of one list in another with different object data types, LINQ?

  5. 5

    List of pointers to different types of objects

  6. 6

    How to join list with different types of elements in python?

  7. 7

    How to list and count the different types of node and edge entities in the graph data using SPARQL query?

  8. 8

    List of template classes of different types

  9. 9

    Share data with different types in UIActivityViewController

  10. 10

    Python - "Joining" list of lists of different types

  11. 11

    How to parse xml with simple-xml library ,where data is list and consist of different types of list?

  12. 12

    Filter in List with custom data types

  13. 13

    Sorting objects of different types into one list

  14. 14

    Folding a list of different types using Shapeless in Scala

  15. 15

    How could a List contains different types in java?

  16. 16

    Storing a list of different generic types in a class

  17. 17

    A more pythonic (or pandorable) way to change a list of columns to different data types

  18. 18

    Mutable list with different types of data

  19. 19

    Private propertys and mutable types

  20. 20

    Different Data types Order By in SQL

  21. 21

    How to list and count the different types of node and edge entities in the graph data using SPARQL query?

  22. 22

    Object data with 2 different types

  23. 23

    Operations on different data types

  24. 24

    How to parse xml with simple-xml library ,where data is list and consist of different types of list?

  25. 25

    Filter in List with custom data types

  26. 26

    A more pythonic (or pandorable) way to change a list of columns to different data types

  27. 27

    TreeTableView: Displaying different data types

  28. 28

    Sort a list of lists in Python with different data types

  29. 29

    Put Different Data Types in a Dynamic List

HotTag

Archive