Scala Set (collection)
Scala Set (collection) is not repeated collection of objects, all elements are unique.
Scala collection is divided into variable and immutable collections.
By default, Scala using immutable collections, if you want to use the variable set, you need to referencescala.collection.mutable.Set package.
The default reference scala.collection.immutable.Set, immutable set of examples are as follows:
val set = Set(1,2,3) println(set.getClass.getName) // println(set.exists(_ % 2 == 0)) //true println(set.drop(1)) //Set(2,3)
If you need to use a variable collection need to introduce scala.collection.mutable.Set:
import scala.collection.mutable.Set // 可以在任何地方引入 可变集合 val mutableSet = Set(1,2,3) println(mutableSet.getClass.getName) // scala.collection.mutable.HashSet mutableSet.add(4) mutableSet.remove(1) mutableSet += 5 mutableSet -= 2 println(mutableSet) // Set(5, 3, 4) val another = mutableSet.toSet println(another.getClass.getName) // scala.collection.immutable.Set
Note: Although variable and immutable Set Set has add or delete elements, but there is a very big difference.An immutable Set operation, will produce a new set, the original set has not changed, which is the same as List. The operation of the variable Set change is that the Set itself, similar to ListBuffer.
Collection of basic operations
Scala collection has three basic operations:
-
head
returns the first element of the collection -
tail
returns a collection that contains in addition to the other elements of the first element -
isEmpty
returns true if the collection is empty
Any operation for Scala collections can be used to express the three basic operations. Examples are as follows:
object Test { def main(args: Array[String]) { val site = Set("w3big", "Google", "Baidu") val nums: Set[Int] = Set() println( "第一网站是 : " + site.head ) println( "最后一个网站是 : " + site.tail ) println( "查看列表 site 是否为空 : " + site.isEmpty ) println( "查看 nums 是否为空 : " + nums.isEmpty ) } }
Implementation of the above code, the output is:
$ vim Test.scala $ scala Test.scala 第一网站是 : w3big 最后一个网站是 : Set(Google, Baidu) 查看列表 site 是否为空 : false 查看 nums 是否为空 : true
Connection set
You can use the++ operator or Set. ++ ()Method to connect two sets. If there are duplicate elements will remove duplicate elements. Examples are as follows:
object Test { def main(args: Array[String]) { val site1 = Set("w3big", "Google", "Baidu") val site2 = Set("Faceboook", "Taobao") // ++ 作为运算符使用 var site = site1 ++ site2 println( "site1 ++ site2 : " + site ) // ++ 作为方法使用 site = site1.++(site2) println( "site1.++(site2) : " + site ) } }
Implementation of the above code, the output is:
$ vim Test.scala $ scala Test.scala site1 ++ site2 : Set(Faceboook, Taobao, Google, Baidu, w3big) site1.++(site2) : Set(Faceboook, Taobao, Google, Baidu, w3big)
Find the maximum and minimum elements in the collection
You can useSet.min method to find the smallest element in the collection, use Set.maxway to find the largest element in the collection. Examples are as follows:
object Test { def main(args: Array[String]) { val num = Set(5,6,9,20,30,45) // 查找集合中最大与最小元素 println( "Set(5,6,9,20,30,45) 集合中的最小元素是 : " + num.min ) println( "Set(5,6,9,20,30,45) 集合中的最大元素是 : " + num.max ) } }
Implementation of the above code, the output is:
$ vim Test.scala $ scala Test.scala Set(5,6,9,20,30,45) 集合中的最小元素是 : 5 Set(5,6,9,20,30,45) 集合中的最大元素是 : 45
Intersection
You can use theSet. & Set.intersectmethod or methods to see the intersection of the two sets of elements. Examples are as follows:
object Test { def main(args: Array[String]) { val num1 = Set(5,6,9,20,30,45) val num2 = Set(50,60,9,20,35,55) // 交集 println( "num1.&(num2) : " + num1.&(num2) ) println( "num1.intersect(num2) : " + num1.intersect(num2) ) } }
Implementation of the above code, the output is:
$ vim Test.scala $ scala Test.scala num1.&(num2) : Set(20, 9) num1.intersect(num2) : Set(20, 9)
Scala Set common method
The following table lists the Scala Set commonly used methods:
No. | Method and Description |
---|---|
1 | def + (elem: A): Set [A] Add a new element to the collection, x and create a new collection, unless the existing elements |
2 | def - (elem: A): Set [A] Removing elements of the collection, and create a new collection |
3 | def contains (elem: A): Boolean If the element is present in the collection, it returns true, otherwise returns false. |
4 | def & (that: Set [A ]): Set [A] Returns the intersection of two sets |
5 | def & ~ (that: Set [ A]): Set [A] Returns the set difference of two sets |
6 | def + (elem1: A, elem2 : A, elems: A *): Set [A] By adding incoming elements of the specified collection to create a new set of immutable |
7 | def ++ (elems: A): Set [A] Merging two collections |
8 | def - (elem1: A, elem2 : A, elems: A *): Set [A] By removing the elements of the specified collection passed to create a new set of immutable |
9 | def addString (b: StringBuilder): StringBuilder Add all the elements of the collection to the immutable string buffer |
10 | def addString (b: StringBuilder, sep : String): StringBuilder Add all the elements of an immutable collection into a string buffer, using the specified delimiter |
11 | def apply (elem: A) Detecting whether the collection contains the specified element |
12 | def count (p: (A) => Boolean): Int Calculate the number of elements that meet the specified criteria set |
13 | def copyToArray (xs: Array [A ], start: Int, len: Int): Unit Copy immutable collection element to an array |
14 | def diff (that: Set [A ]): Set [A] Difference sets comparing two sets |
15 | def drop (n: Int): Set [A]] The new collection before returning discards n elements |
16 | def dropRight (n: Int): Set [A] Back discard the last n elements of the new collection |
17 | def dropWhile (p: (A) => Boolean): Set [A] Discard element left to right, until the condition is not satisfied p |
18 | def equals (that: Any): Boolean equals method can be used in any sequence. For comparing the series are equal. |
19 | def exists (p: (A) => Boolean): Boolean Analyzing immutable set the conditions specified element exists. |
20 | def filter (p: (A) => Boolean): Set [A] Output meets all specified criteria immutable collection elements. |
twenty one | def find (p: (A) => Boolean): Option [A] Finding the first element in the collection immutable meet the specified criteria |
twenty two | def forall (p: (A) => Boolean): Boolean Find all the elements of an immutable collection that meets the specified criteria |
twenty three | def foreach (f: (A) => Unit): Unit The function is applied to all elements of the immutable collection |
twenty four | def head: A Get the first element of the collection immutable |
25 | def init: Set [A] Returns all elements, except the last one |
26 | def intersect (that: Set [A ]): Set [A] Compute the intersection of two sets |
27 | def isEmpty: Boolean Determine whether the collection is empty |
28 | def iterator: Iterator [A] Create a new iterator to iterate the elements |
29 | def last: A Returns the last element |
30 | def map [B] (f: (A) => B): immutable.Set [B] Through a given method will recalculate all the elements |
31 | def max: A Find the maximum element |
32 | def min: A Find the smallest element |
33 | def mkString: String The set of all elements of the display as a string |
34 | def mkString (sep: String): String Separators will bring together all the elements displayed as a string |
35 | def product: A Returns an immutable set of numbers in the plot elements. |
36 | def size: Int Returns the number of elements in the collection immutable |
37 | def splitAt (n: Int): (Set [A], Set [A]) The immutable collection is split into two containers, the first by the first n elements, and the second by the remaining elements |
38 | def subsetOf (that: Set [A ]): Boolean If the collection contains a subset returns true, false otherwise |
39 | def sum: A Returns an immutable set of all elements of the digital and |
40 | def tail: Set [A] Returns an immutable set of other elements in addition to the first element |
41 | def take (n: Int): Set [A] Returns the first n elements |
42 | def takeRight (n: Int): Set [A] n elements Back |
43 | def toArray: Array [A] Will be converted to a digital collection |
44 | def toBuffer [B>: A] : Buffer [B] Back buffer, contains all the elements of a collection of immutable |
45 | def toList: List [A] Back List, contains all the elements of a collection of immutable |
46 | def toMap [T, U]: Map [T, U] Back Map, contains all the elements of a collection of immutable |
47 | def toSeq: Seq [A] Back Seq, contains all the elements of a collection of immutable |
48 | def toString (): String Returns a string to object to represent |
More ways to refer to the API documentation