美文网首页禅与计算机程序设计艺术
associateBy 和 groupBy 之间的区别

associateBy 和 groupBy 之间的区别

作者: 光剑书架上的书 | 来源:发表于2019-08-07 15:19 被阅读3次

    函数associateBy和groupBy构建来自由指定键索引的集合的元素的映射。key在keySelector参数中定义。

    您还可以指定可选的valueSelector来定义将存储在map元素值中的内容。

    区别

    associateBy和groupBy之间的区别在于它们如何使用相同的键处理对象:

    • associateBy使用最后一个合适的元素作为值。
    • groupBy构建所有合适元素的列表并将其放入值中。

    associateBy

    /**
     * Returns a [Map] containing the elements from the given collection indexed by the key
     * returned from [keySelector] function applied to each element.
     * 
     * If any two elements would have the same key returned by [keySelector] the last one gets added to the map.
     * 
     * The returned map preserves the entry iteration order of the original collection.
     */
    public inline fun <T, K> Iterable<T>.associateBy(keySelector: (T) -> K): Map<K, T> {
        val capacity = mapCapacity(collectionSizeOrDefault(10)).coerceAtLeast(16)
        return associateByTo(LinkedHashMap<K, T>(capacity), keySelector)
    }
    
    
    /**
     * Populates and returns the [destination] mutable map with key-value pairs,
     * where key is provided by the [keySelector] function applied to each element of the given collection
     * and value is the element itself.
     * 
     * If any two elements would have the same key returned by [keySelector] the last one gets added to the map.
     */
    public inline fun <T, K, M : MutableMap<in K, in T>> Iterable<T>.associateByTo(destination: M, keySelector: (T) -> K): M {
        for (element in this) {
            destination.put(keySelector(element), element)
        }
        return destination
    }
    
    

    groupBy

    /**
     * Groups elements of the original collection by the key returned by the given [keySelector] function
     * applied to each element and returns a map where each group key is associated with a list of corresponding elements.
     * 
     * The returned map preserves the entry iteration order of the keys produced from the original collection.
     * 
     * @sample samples.collections.Collections.Transformations.groupBy
     */
    public inline fun <T, K> Iterable<T>.groupBy(keySelector: (T) -> K): Map<K, List<T>> {
        return groupByTo(LinkedHashMap<K, MutableList<T>>(), keySelector)
    }
    
    
    
    /**
     * Groups elements of the original collection by the key returned by the given [keySelector] function
     * applied to each element and puts to the [destination] map each group key associated with a list of corresponding elements.
     * 
     * @return The [destination] map.
     * 
     * @sample samples.collections.Collections.Transformations.groupBy
     */
    public inline fun <T, K, M : MutableMap<in K, MutableList<T>>> Iterable<T>.groupByTo(destination: M, keySelector: (T) -> K): M {
        for (element in this) {
            val key = keySelector(element)
            val list = destination.getOrPut(key) { ArrayList<T>() }
            list.add(element)
        }
        return destination
    }
    

    Kotlin 开发者社区

    国内第一Kotlin 开发者社区公众号,主要分享、交流 Kotlin 编程语言、Spring Boot、Android、React.js/Node.js、函数式编程、编程思想等相关主题。

    Kotlin 开发者社区

    相关文章

      网友评论

        本文标题:associateBy 和 groupBy 之间的区别

        本文链接:https://www.haomeiwen.com/subject/ozwodctx.html