📜  Scala集

📅  最后修改于: 2021-01-09 12:05:03             🧑  作者: Mango

斯卡拉集

它用于在集合中存储唯一元素。它不维护任何存储元素的顺序。您可以对它们执行各种操作。它在Scala.collection.immutable包中定义。

Scala设置语法

val variableName:Set[Type] = Set(element1, element2,... elementN) or
val variableName = Set(element1, element2,... elementN)

Scala设置示例

在此示例中,我们创建了一个集合。您也可以创建一个空集。让我们看看如何创建一个集合。

import scala.collection.immutable._
object MainObject{
    def main(args:Array[String]){
        val set1 = Set()                            // An empty set
        val games = Set("Cricket","Football","Hocky","Golf")     // Creating a set with elements
        println(set1)
        println(games)
    }
}

输出:

Set()    // an empty set
Set(Cricket,Football,Hocky,Golf)

Scala设置示例2

在Scala中,Set提供了一些预定义的属性来获取有关set的信息。您可以获取Set的第一个或最后一个元素以及更多其他元素。让我们来看一个例子。

import scala.collection.immutable._
object MainObject{
        def main(args:Array[String]){
            val games = Set("Cricket","Football","Hocky","Golf")
            println(games.head)                // Returns first element present in the set
            println(games.tail)            // Returns all elements except first element.
            println(games.isEmpty)            // Returns either true or false
        }
    }

输出:

Cricket
Set(Football, Hocky, Golf)
false

Scala Set示例:合并两个Set

您可以将两个集合合并为一个集合。 Scala提供了一种预定义的方法来合并集合。在此示例中,++方法用于合并两个集合。

import scala.collection.immutable._
object MainObject{
        def main(args:Array[String]){
            val games = Set("Cricket","Football","Hocky","Golf")
            val alphabet = Set("A","B","C","D","E") 
            val mergeSet = games ++ alphabet            // Merging two sets
            println("Elements in games set: "+games.size)    // Return size of collection
            println("Elements in alphabet set: "+alphabet.size)    
            println("Elements in mergeSet: "+mergeSet.size)
            println(mergeSet)
        }
    }

输出:

Elements in games set: 4
Elements in alphabet set: 5
Elements in mergeSet: 9
Set(E, Football, Golf, Hocky, A, B, C, Cricket, D)

此示例还证明了合并集不维护存储元素的顺序。

Scala设置示例2

您可以检查元素是否存在于集合中。以下示例描述了contains()方法的用法。

import scala.collection.immutable._
object MainObject{
        def main(args:Array[String]){
            val games = Set("Cricket","Football","Hocky","Golf")
            println(games)
            println("Elements in set: "+games.size)
            println("Golf exists in the set : "+games.contains("Golf"))
            println("Racing exists in the set : "+games.contains("Racing"))
            
        }
    }

输出:

Set(Cricket, Football, Hocky, Golf)
Elements in set: 4
Golf exists in the set : true
Racing exists in the set : false

Scala设置示例:添加和删除元素

您可以从集合中添加或删除元素。您只能在代码可变时添加。在此示例中,我们将添加和删除集合中的元素。

import scala.collection.immutable._
object MainObject{
        def main(args:Array[String]){
            var games = Set("Cricket","Football","Hocky","Golf")
            println(games)
            games += "Racing"               // Adding new element
            println(games)
            games += "Cricket"               // Adding new element, it does not allow duplicacy.
            println(games)
            games -= "Golf"             // Removing element
            println(games)
        }
    }

输出:

Set(Cricket, Football, Hocky, Golf)
Set(Football, Golf, Hocky, Cricket, Racing)
Set(Football, Golf, Hocky, Cricket, Racing)
Set(Football, Hocky, Cricket, Racing)

Scala Set示例:使用for循环迭代Set元素

您可以使用for循环或foreach循环来迭代set元素。您还可以在迭代过程中过滤元素。在此示例中,使用了for循环来迭代set元素。

import scala.collection.immutable._
object MainObject{
        def main(args:Array[String]){
            var games = Set("Cricket","Football","Hocky","Golf")
            for(game <- games){
                println(game)
            }
        }
    }

输出:

Cricket
Football
Hocky
Golf

使用foreach循环的Scala集示例迭代元素

在此示例中,我们使用foreach循环来迭代set元素。

import scala.collection.immutable._
    object MainObject{
        def main(args:Array[String]){
            var games = Set("Cricket","Football","Hocky","Golf")
            games.foreach((element:String)=> println(element))
        }   
}

输出:

Cricket
Football
Hocky
Golf

Scala设置示例:设置操作

在scala Set中,您还可以使用典型的数学运算,例如:相交和并集。在下面的示例中,我们使用了预定义的方法来执行设置操作。

import scala.collection.immutable._
object MainObject{
    def main(args:Array[String]){
        var games = Set("Cricket","Football","Hocky","Golf","C")
        var alphabet = Set("A","B","C","D","E","Golf")
        var setIntersection = games.intersect(alphabet)
        println("Intersection by using intersect method: "+setIntersection)
        println("Intersection by using & operator: "+(games & alphabet))
        var setUnion = games.union(alphabet)
        println(setUnion)
    }
}

输出:

Intersection by using intersect method: Set(Golf, C)
Intersection by using & operator: Set(Golf, C)
Set(E, Football, Golf, Hocky, A, B, C, Cricket, D)

Scala SortedSet

在scala中,SortedSet扩展了Set特征并提供了已排序的Set元素。当您要对Set集合中的元素进行排序时,此功能很有用。您还可以对整数值和字符串进行排序。

它是一个特征,您可以应用在可遍历特征和Set特征中定义的所有方法。

Scala SortedSet示例

在下面的示例中,我们使用SortedSet来存储整数元素。排序元素后返回Set。

import scala.collection.immutable.SortedSet            
object MainObject{
    def main(args:Array[String]){
        var numbers: SortedSet[Int] = SortedSet(5,8,1,2,9,6,4,7,2)
        numbers.foreach((element:Int)=> println(element))
    }   
}

输出:

1
2
4
5
6
7
8
9