scala 创建可变集合
Scala集 (Scala Set)
In Scala, a Set is a collection of elements of the same type. All elements of the set are unique i.e. no elements are allowed. Sets can be mutable as well as immutable.
在Scala中,集合是相同类型元素的集合。 集合中的所有元素都是唯一的,即不允许任何元素。 集可以是可变的,也可以是不变的。
Example:
例:
Set(1, 4, 5, 7, 12, 87, 213)
The mutable set is a set that contains elements that can be changed after the initialization of the set.
可变集是一个包含元素的集合,这些元素可以在初始化之后更改。
A mutable variable in the Scala is declared using the val keyword. And mutable Sets in scala are imported from,
Scala中的可变变量是使用val关键字声明的。 Scala中的可变集是从
scala.collection.mutable.Set
Example:
例:
object MyClass {
def main(args: Array[String]) {
val set = scala.collection.mutable.Set(2, 56, 577,12 , 46, 19);
println("The set is : "+set)
set += 34;
set += 99;
println("After adding two elements, the set is "+ set)
}
}
Output
输出量
The set is : HashSet(577, 2, 19, 56, 12, 46)
After adding two elements, the set is HashSet(577, 2, 34, 19, 99, 56, 12, 46)
翻译自: https://www.includehelp.com/scala/how-to-create-a-mutable-set-in-scala.aspx
scala 创建可变集合