How to partition and typecast a List in Kotlin

爱⌒轻易说出口 提交于 2020-01-04 13:35:18

问题


In Kotlin I can:

val (specificMembers, regularMembers) = members.partition {it is SpecificMember}

However to my knowledge I can not do something like:

val (specificMembers as List<SpecificMember>, regularMembers) = members.partition {it is SpecificMember}

My question would be - is there's an idiomatic way to partition iterable by class and typecast it those partitioned parts if needed.


回答1:


The partition function will return a Pair<List<T>, List<T>> with T being the generic type of your Iterable. You can transform the partitioned values again using e.g. let:

val (specificMembers, regularMembers) = lists
    .partition { it is SpecificMember }
    .let { Pair(it.first as List<SpecificMember>, it.second) }



回答2:


If you require that functionality more often, you may just reimplement the actual partition according to your needs, e.g.:

inline fun <reified U : T, T> Iterable<T>.partitionByType(): Pair<List<U>, List<T>> {
  val first = ArrayList<U>()
  val second = ArrayList<T>()
  for (element in this) {
    if (element is U) first.add(element)
    else second.add(element)
  }
  return Pair(first, second)
}

with a usage similar as to follows:

val (specificMembers, regularMembers) = members.partitionByType<SpecificMember, Member>()
// where specificMembers : List<SpecificMember>
// and regularMembers : List<Member> for this example

Note that this way you can also set the second type to a more generic one. I leave that up to you whether this makes sense. At least this way an unchecked cast isn't necessary.

The alternative is also shown by Simon with the let-usage. You can also directly cast the result of partition (without let and another Pair) to whatever fits, e.g.:

val (specificMembers, regularMembers) = members.partition {it is SpecificMember} as Pair<List<SpecificMember>, List<Member>>


来源:https://stackoverflow.com/questions/56325366/how-to-partition-and-typecast-a-list-in-kotlin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!