scala 集合的层级结构
Scala集合层次结构 (Scala Collection Hierarchy)
Scala collections have a rich hierarchy. The traversable trait is at the root of Scala hierarchy, all classes inherit some traits that are required for the general functioning of the collections.
Scala集合具有丰富的层次结构。 可遍历特征是Scala层次结构的根本,所有类都继承了集合的一般功能所需的某些特征。
The below figure shows the collection hierarchy:
下图显示了集合层次结构 :
![Collection Hierarchy in Scala](https://i-blog.csdnimg.cn/blog_migrate/3c7170be35bddb13623b2768137516b0.png)
Traversable Trait
可穿越的特质
At the root, there is Traversable trait that implements a common method to all collections to traverse the collections.
从根本上讲,存在Traversable特性,该特性为所有集合实现了遍历集合的通用方法。
Iterable trait
可重复性状
There is also a common trait for all collection classes used to iterate over the elements of a collection class by defining Iterator.
通过定义Iterator来迭代集合类元素的所有集合类也有一个共同特征。
Inheriting common features for Traversable and iterable there are three main categories:
继承Traversable和Iterable的常用功能,主要分为三类:
Sequence (Seq)
序列(Seq)
Sets(Set)
套(套)
Maps(Map)
地图(地图)
1) Sequence
1)顺序
Sequence or Seq is a trait of iterable class that has defined the order of elements. This immutable trait provides indexing of elements in it. Some common seq methods are:
Sequence或Seq是可迭代类的特征,它定义了元素的顺序。 这种不可变的特征为其中的元素提供了索引。 一些常见的seq方法是:
copyToarray() : copies elements of Seq to an array
copyToarray() :将Seq的元素复制到数组
indexOf() : finds the index of element passed
indexOf() :查找传递的元素的索引
reverse() : return a new sequence with reverse order
reverse() :返回具有相反顺序的新序列
inEmpty() : returns true if code is empty
inEmpty() :如果代码为空,则返回true
indexWhere(element,from) : checks for index of element for the nth place
indexWhere(element,from) :检查第n个元素的索引
Max() : finds the maximum element of the sequence
Max() :找到序列的最大元素
Min() : finds the minimum element of the sequence
Min() :找到序列的最小元素
2) Sets
2)套
Scala Sets are a collection of different elements of the same type. There are no duplicate data elements in sets. Scala sets are mutable/ immutable based on how objects are changed? Sets are extended scala.collection.immutable or scala.collection.mutable package. Some common Sets methods are:
Scala集是同一类型的不同元素的集合。 集合中没有重复的数据元素。 Scala集根据对象的更改是可变的/不可变的? 集是扩展的scala.collection.immutable或scala.collection.mutable包。 一些常见的Set方法是:
count() : counts the number of elements based on a condition
count() :根据条件计算元素数
max[B :> A] : finds the largest element
max [B:> A] :找到最大的元素
min [B :> A] : finds the smallest element
min [B:> A] :找到最小的元素
Size : output the size of collection
大小 :输出集合的大小
toString() : creates a string representation on the object
toString() :在对象上创建一个字符串表示形式
copytoArray() : Creates an array with elements of the array
copytoArray() :使用数组的元素创建一个数组
3) Maps
3)地图
The map is a collection of key/value pairs. The value of an element is pointed by its key like an index but the key can be anything. Generally, maps in Scala are immutable type and its objects cannot be changed. But if required, the programmer can use scala.collection.mutable.map package to use the mutable version of it.
该映射是键/值对的集合。 元素的值由其键(如索引)指出,但键可以是任何东西。 通常,Scala中的地图是不可变的类型,其对象无法更改。 但是,如果需要,程序员可以使用scala.collection.mutable.map包使用其可变版本。
Methods that are valid for Maps trait are:
对于“ 地图”特征有效的方法是:
def iterator : Returns a new iterator over the map
def iterator :在地图上返回一个新的迭代器
clone() : makes a copy of the map
clone() :复制地图
last() : returns the last element of map
last() :返回地图的最后一个元素
get() : gets the value associated with the key
get() :获取与键关联的值
toSet() : Creates a set with all elements of the map
toSet() :创建一个包含地图所有元素的集合
toString() : return a string with elements
toString() :返回包含元素的字符串
翻译自: https://www.includehelp.com/scala/collection-hierarchy-in-scala.aspx
scala 集合的层级结构