What is the best way to fixed size of List in Kotlin

15,038

You can create List or Array with fixed size, but in that case you need to initialize it, as val childList = arrayOfNulls<ChildObject>(6) (no other option to have an array with fixed size without initializing it), and in your loop check if it full (or when last element added) and return from method.

In that case yo don't need to loop over all parent elements and after to cut it, just do the simple check.

Share:
15,038

Related videos on Youtube

Bulma
Author by

Bulma

Never give up !

Updated on June 04, 2022

Comments

  • Bulma
    Bulma almost 2 years

    I have a list of ParentObject. Foreach parentObject, it has 2 childObject. The image like that

    val listParent: MutableList<ParentObject> = mutableList() 
    
    ParentObject {  
     ChildOjbect1{}   // object1 can not be NULL
     ChildOjbect2{}   // object2 can be NULL
    }
    

    And, I want to build a mutableList of ChildObject.

    val listChild: MutableList<ChildObject> = mutableList()  
    
    list.add(parentObject.childOjbect1) // Because childObj1 can not be null  
    parentOjbect.childObject2?.let { child ->  
      list.add(child)  
    }
    

    Question:
    I only need listChild with 6 items ( Basically I'd like to fixed size of listChild is 6 )
    I am coding as below in Kotlin:

    fun buildListChild(): List<ChildOjbect> {
      val listChild // instance mutable listChild
      forEach( parent: listParent) {
        listChild.add(parent.childObject1)
        parent.childOjbect2?.let {  it ->
        listChild.add(it)
      }
      return listChild.take(6)
    }
    

    I think performance is not good, because it loop in all of parent item. Beside that I really don't want to always check size of listChild before add.
    What is the best way to solve problem here ?

  • Bulma
    Bulma over 5 years
    It is same solution I was thinking. I really don’t want to always check size of list before add !!!
  • mr.kostua
    mr.kostua over 5 years
    I think in any case you need to do some check to know when to return from method so you will not perform unnecessary loop iterations. It can do some external library that throws exception when list is full (size is 6) but still they will do same size check inside. If you want some clear code solution create your custom List class with proper methods.
  • Alexey Romanov
    Alexey Romanov over 5 years
    instead of map(something).flatMap {it}, just use flatMap(something).
  • Bulma
    Bulma over 5 years
    @MahdiPerfect as you said, we still have to loop in all item of parent list ? Is it not a waste of time ?
  • Bulma
    Bulma over 5 years
    @kostua Hmmm, maybe we have to check size of child list every time before add. It looks like: if (listChild.size < 6) { add child 1 } parent.child2?.let { if(listChild.size < 6 ) add child2 }
  • Bulma
    Bulma over 5 years
    .flatMap{it -> listOf(parent.childObject1, parent.childOjbect2)} // Note that , childObject2 can be null. We have to check here ???