scala set
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中,集合是相同类型元素的集合。 集合中的所有元素都是唯一的,即不允许任何元素。 集可以是可变的,也可以是不变的。
SortedSet
SortedSet
It is a set in which all elements of the set are arranged in sorted order.
它是一个集合,其中集合的所有元素都按排序顺序排列。
Example:
例:
{1, 4 , 7, 9, 10, 12, 24, 65, 90}
To convert a set into sortedSet, there are multiple methods,
要将集合转换为sortedSet,有多种方法,
object MyClass {
def main(args: Array[String]) {
val set = Set(2, 56, 577,12 , 46,9, 90, 19);
println("The set is : "+ set)
val sortedSet = collection.immutable.SortedSet[Int]() ++ set
println("The sorted set is : "+ sortedSet)
var sortedSet2 = collection.immutable.TreeSet[Int]() ++ set
println("The sorted set is : "+ sortedSet2)
var sortedSet3 = collection.mutable.SortedSet(set.toList: _*)
println("The sorted set is : "+ sortedSet3)
}
}
Output
输出量
The set is : HashSet(56, 46, 9, 2, 577, 12, 19, 90)
The sorted set is : TreeSet(2, 9, 12, 19, 46, 56, 90, 577)
The sorted set is : TreeSet(2, 9, 12, 19, 46, 56, 90, 577)
The sorted set is : TreeSet(2, 9, 12, 19, 46, 56, 90, 577)
The first two methods that are used here ("SortedSet" and "TreeSet") are used to sort immutable sets in Scala and take set as input and return the sorted set.
这里使用的前两个方法( “ SortedSet”和“ TreeSet” )用于对Scala中的不可变集进行排序,并以set作为输入并返回已排序的集。
The last method is SortedSet working over mutable sets too and take the list conversion of the set to sort.
最后一个方法是SortedSet,它也处理可变集,并将该集的列表转换为sort。
翻译自: https://www.includehelp.com/scala/how-to-convert-a-set-to-a-sortedset-in-scala.aspx
scala set