scala bitset
Scala BitSet (Scala BitSet)
Set is a collection of unique elements.
集合是唯一元素的集合。
Bitset is a set of positive integers represented as a 64-bit word.
位集是一組表示為64位字的正整數。
Syntax:
句法:
var bitset : Bitset = Bitset(elements...)
In the Scala programming language, BitSet can be mutable as well as immutable.
在Scala編程語言中, BitSet可以是可變的而且是不變的。
In mutable BitSet, bits can be changed in the program. Using scala.collection.mutable.BitSet
在可變的BitSet中 ,可以在程序中更改位。 使用scala.collection.mutable.BitSet
In immutable BitSet, bits cannot be changed in the program. Using scala.collection.immutable.BitSet
在不可變的BitSet中 ,不能在程序中更改位。 使用scala.collection.immutable.BitSet
Example 1: Creation of a new BitSet
示例1:創建一個新的BitSet
import scala.collection.immutable.BitSet
object MyClass {
def main(args: Array[String]) {
val bitSet: BitSet = BitSet(0, 1, 2, 3)
println("Elements of new BitSet are " + bitSet)
}
}
Output
輸出量
Elements of new BitSet are BitSet(0, 1, 2, 3)
Example 2: Search for an element in BitSet
示例2:在BitSet中搜索元素
Search operation on BitSet in Scala is quite easy and passing the elements to be searched in BitSet, and it will return true or false based on the search.
在Scala中對BitSet進行搜索操作非常容易,并且可以在BitSet中傳遞要搜索的元素,并且根據搜索結果將返回true或false。
import scala.collection.immutable.BitSet
object MyClass {
def main(args: Array[String]) {
val bitSet: BitSet = BitSet(0, 13, 25, 39, 50)
println("Elements of new BitSet are " + bitSet)
println("Element 25 is in the BitSet? " + bitSet(25))
println("Element 34 is in the BitSet? " + bitSet(34))
}
}
Output
輸出量
Elements of new BitSet are BitSet(0, 13, 25, 39, 50)
Element 25 is in the BitSet? true
Element 34 is in the BitSet? false
Example 3: Adding elements to the BitSet
示例3:將元素添加到BitSet
You can add one or multiple elements in a BitSet in Scala. The operators + and ++ are used to add single and multiple elements in BitSet in Scala. The operation will require new BitSet to hold the updated BitSet.
您可以在Scala的BitSet中添加一個或多個元素。 運算符+和++用于在Scala的BitSet中添加單個和多個元素。 該操作將需要新的BitSet來保存更新的BitSet。
import scala.collection.immutable.BitSet
object MyClass {
def main(args: Array[String]) {
val bitSet: BitSet = BitSet(0, 13, 25, 39, 50)
println("Elements of new BitSet are " + bitSet)
println("Adding new elements to BitSet : ")
val bitSet2 : BitSet = bitSet + 45
println("Elements of new BitSet are " + bitSet2)
println("Adding new elements to BitSet : ")
val bitSet3 : BitSet = bitSet2 ++ BitSet(34 , 54)
println("Elements of new BitSet are " + bitSet3)
}
}
Output
輸出量
Elements of new BitSet are BitSet(0, 13, 25, 39, 50)
Adding new elements to BitSet :
Elements of new BitSet are BitSet(0, 13, 25, 39, 45, 50)
Adding new elements to BitSet :
Elements of new BitSet are BitSet(0, 13, 25, 34, 39, 45, 50, 54)
Example 4: Deleting Elements from BitSet in Scala
示例4:從Scala的BitSet中刪除元素
You can delete elements from BitSet in Scala. The operator - is used to delete an element from BitSet. The operations will require new BitSet to hold the updated BitSet.
您可以從Scala的BitSet中刪除元素。 運算符-用于從BitSet中刪除元素。 該操作將需要新的BitSet來保存更新的BitSet。
import scala.collection.immutable.BitSet
object MyClass {
def main(args: Array[String]) {
val bitSet: BitSet = BitSet(0, 13, 25, 39, 50)
println("Elements of new BitSet are " + bitSet)
println("Deleting element from BitSet : ")
val bitSet2 : BitSet = bitSet - 25
println("Elements of new BitSet are " + bitSet2)
}
}
Output
輸出量
Elements of new BitSet are BitSet(0, 13, 25, 39, 50)
Deleting element from BitSet :
Elements of new BitSet are BitSet(0, 13, 39, 50)
Example 5: Creating Empty BitSet in Scala
示例5:在Scala中創建空的BitSet
An empty set can also be created in Scala. The empty keyword is used to create an empty BitSet in Scala.
也可以在Scala中創建一個空集。 empty關鍵字用于在Scala中創建一個空的BitSet。
import scala.collection.immutable.BitSet
object MyClass {
def main(args: Array[String]) {
val bitSet: BitSet = BitSet.empty
println("Elements of new BitSet are " + bitSet)
}
}
Output
輸出量
Elements of new BitSet are BitSet()
翻譯自: https://www.includehelp.com/scala/bitset.aspx
scala bitset