map

map就是将transform方法产生的结果添加到一个新的集合里面去,然后返回这个新的集合

  1. /**
  2. * Returns a list containing the results of applying the given [transform] function
  3. * to each element in the original collection.
  4. *
  5. * @sample samples.collections.Collections.Transformations.map
  6. */
  7. public inline fun <T, R> Iterable<T>.map(transform: (T) -> R): List<R> {
  8. return mapTo(ArrayList<R>(collectionSizeOrDefault(10)), transform)
  9. }
  10. public inline fun <T, R, C : MutableCollection<in R>> Iterable<T>.mapTo(destination: C, transform: (T) -> R): C {
  11. for (item in this)
  12. destination.add(transform(item))
  13. return destination
  14. }

flatten

flatten 处理嵌套集合,遍历这个嵌套集合,将每一个子集合中的元素通过addAll方法添加到新的集合中,最终得到一个扁平化的集合

/**
 * Returns a single list of all elements from all collections in the given collection.
 * @sample samples.collections.Iterables.Operations.flattenIterable
 */
public fun <T> Iterable<Iterable<T>>.flatten(): List<T> {
    val result = ArrayList<T>()
    for (element in this) {
        result.addAll(element)
    }
    return result
}

flatMap

flatMap 遍历集合中的元素,然后将每个元素传入transform中处理后得到一个列表,将这个列表的所有元素添加到destination中,最终得到一个扁平化的列表

/**
 * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original collection.
 */
public inline fun <T, R> Iterable<T>.flatMap(transform: (T) -> Iterable<R>): List<R> {
    return flatMapTo(ArrayList<R>(), transform)
}

public inline fun <T, R, C : MutableCollection<in R>> Iterable<T>.flatMapTo(destination: C, transform: (T) -> Iterable<R>): C {
    for (element in this) {
        val list = transform(element)
        destination.addAll(list)
    }
    return destination
}

可见,flatMap其实可以看作由flatten和map进行组合之后的方法,组合方式根据具体情况来定。 仅仅是对一个集合进行扁平化操作时,使用flatten就可以了。 如果需要对其中的元素进行一些“加工”,可以考虑使用flatMap