<commit_before>---
type: doc
layout: reference
category: "Collections"
title: "集合操作概述"
---

# 集合操作概述

The Kotlin standard library offers a broad variety of functions for performing operations on collections. This includes simple operations, such as getting or adding elements, as well as more complex ones including search, sorting, filtering, transformations, and so on.  

## 扩展与成员函数

Collection operations are declared in the standard library in two ways: [member functions](classes.html#类成员) of collection interfaces and [extension functions](extensions.html#扩展函数).

Member functions define operations that are essential for a collection type. For example, [`Collection`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-collection/index.html) contains the function [`isEmpty()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-collection/is-empty.html) for checking its emptiness; [`List`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html) contains [`get()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/get.html) for index access to elements, and so on.

When you create own implementations of collection interfaces, you must implement their member functions.
To make the creation of new implementations easier, use the skeletal implementations of collection interfaces from the standard library: [`AbstractCollection`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-abstract-collection/index.html), [`AbstractList`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-abstract-list/index.html), [`AbstractSet`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-abstract-set/index.html), [`AbstractMap`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-abstract-map/index.html), and their mutable counterparts.

Other collection operations are declared as extension functions. These are filtering, transformation, ordering, and other collection processing functions. 

## 公共操作

Common operations are available for both [read-only and mutable collections](collections-overview.html#集合类型). Common operations fall into these groups:

* [Transformations](collection-transformations.html)
* [Filtering](collection-filtering.html)
* [`plus` and `minus` operators](collection-plus-minus.html)
* [Grouping](collection-grouping.html)
* [Retrieving collection parts](collection-parts.html)
* [Retrieving single elements](collection-elements.html)
* [Ordering](collection-ordering.html)
* [Aggregate operations](collection-aggregate.html)

Operations described on these pages return their results without affecting the original collection. For example, a filtering operation produces a _new collection_ that contains all the elements matching the filtering predicate.
Results of such operations should be either stored in variables, or used in some other way, for example, passed in other functions.

<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">

```kotlin
fun main() {<commit_msg>集合 - 集合操作概述 - 拓展与成员函数<commit_after>---
type: doc
layout: reference
category: "Collections"
title: "集合操作概述"
---

# 集合操作概述

Kotlin 标准库提供了用于对集合执行操作的多种函数。这包括简单的操作，例如获取或添加元素，以及更复杂的操作，包括搜索、排序、过滤、转换等。

## 扩展与成员函数

集合操作在标准库中以两种方式声明：集合接口的[类成员](classes.html#类成员)和[扩展函数](extensions.html#扩展函数)。

定义成员函数对于集合类型是必不可少的操作。例如，[`Collection`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-collection/index.html) 包含函数 [`isEmpty()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-collection/is-empty.html) 来检查其是否为空； [`List`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/index.html) 包含用于对元素进行索引访问的 [`get()`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/get.html)，等等。

创建自己的集合接口实现时，必须实现其成员函数。
为了使新实现的创建更加容易，请使用标准库中集合接口的框架实现：[`AbstractCollection`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-abstract-collection/index.html)、[`AbstractList`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-abstract-list/index.html)、[`AbstractSet`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-abstract-set/index.html)、[`AbstractMap`](https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-abstract-map/index.html) 及其可变对象。

其他集合操作被声明为扩展函数。这些是过滤、转换、排序和其他集合处理功能。

## 公共操作

Common operations are available for both [read-only and mutable collections](collections-overview.html#集合类型). Common operations fall into these groups:

* [Transformations](collection-transformations.html)
* [Filtering](collection-filtering.html)
* [`plus` and `minus` operators](collection-plus-minus.html)
* [Grouping](collection-grouping.html)
* [Retrieving collection parts](collection-parts.html)
* [Retrieving single elements](collection-elements.html)
* [Ordering](collection-ordering.html)
* [Aggregate operations](collection-aggregate.html)

Operations described on these pages return their results without affecting the original collection. For example, a filtering operation produces a _new collection_ that contains all the elements matching the filtering predicate.
Results of such operations should be either stored in variables, or used in some other way, for example, passed in other functions.

<div class="sample" markdown="1" theme="idea" data-min-compiler-version="1.3">

```kotlin
fun main() {
{'fraction_non_alphanumeric': 0.12465012129128569, 'fraction_numerical': 0.0011196118678857996, 'mean_word_length': 6.3124147339699865, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 16, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4660429', 'n_tokens_mistral': 1811, 'n_tokens_neox': 1828, 'n_words': 376}