scala set_如何在Scala中将Set转换为SortedSet?

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值