📜  Scala 中的位集

📅  最后修改于: 2022-05-13 01:54:27.504000             🧑  作者: Mango

Scala 中的位集

集合是仅包含不可重复的唯一项的集合。 BitSet 是小整数的集合,作为较大整数的位。表示为打包成 64 位字的可变大小位数组的非负整数集称为BitSets 。存储在 bitset 中的最大数是 bitset 的内存。它扩展了 Set 特征。

句法:

var BS : BitSet = BitSet(element1, element2, element3, ....)  

Where BS is the name of created BitSet

在 Scala 中,BitSet 有两个版本: scala.collection.immutable.BitSetscala.collection.mutable.BitSet 。它们几乎相同,但可变版本改变了位,因此不可变数据结构更适合并发性。

使用 BitSet 执行的操作

初始化 BitSet :下面是创建或初始化 BitSet 的示例。

例子 :

// Scala program to initialize a BitSet
import scala.collection.immutable.BitSet
  
// Creating object
object GFG
{ 
    // Main method
    def main(args:Array[String])
    { 
        println("Initialize a BitSet")
          
        // Creating HashSet
        val bitSet: BitSet = BitSet(0, 1, 2, 3)
        println(s"Elements are = $bitSet")
    }
} 
输出:
Initialize a BitSet
Elements are = BitSet(0, 1, 2, 3)


检查 BitSet 中的特定元素:

例子 :

// Scala program of Check specific elements in BitSet
import scala.collection.immutable.BitSet
  
// Creating object
object GFG
{ 
    // Main method
    def main(args:Array[String])
    { 
        println("Initialize a BitSet")
          
        // Creating BitSet
        val bitSet: BitSet = BitSet(0, 1, 2, 3)
        println(s"Elements are = $bitSet")
          
        // Checking
        println(s"Element 2 = ${bitSet(2)}")
        println(s"Element 4 = ${bitSet(4)}")
    }
} 
输出:
Initialize a BitSet
Elements are = BitSet(0, 1, 2, 3)
Element 2 = true
Element 4 = false


在 BitSet 中添加元素:我们可以使用 + 号在 BitSet 中添加元素。下面是在 BitSet 中添加元素的示例。

例子 :

// Scala program of adding an element in BitSet
import scala.collection.immutable.BitSet
  
// Creating object
object GFG
{ 
    // Main method
    def main(args:Array[String])
    { 
        println("Initialize a BitSet")
          
        // Creating BitSet
        val bs: BitSet = BitSet(0, 1, 2, 3)
        println(s"Elements are = $bs")
          
        // Adding an element in BitSet
        val bs1: BitSet = bs + 10 + 11
        println(s"Adding elements to BitSet = $bs1")
    }
}
输出:
Initialize a BitSet
Elements are = BitSet(0, 1, 2, 3)
Adding elements to BitSet = BitSet(0, 1, 2, 3, 10, 11)


在 BitSet 中添加多个元素:我们可以使用 ++ 符号在 BitSet 中添加多个元素。下面是在 BitSet 中添加多个元素的示例。

例子 :

// Scala program of adding more elements in BitSet
import scala.collection.immutable.BitSet
  
// Creating object
object GFG
{ 
    // Main method
    def main(args:Array[String])
    { 
        println("Initialize a BitSet")
          
        // Creating BitSet
        val bs: BitSet = BitSet(0, 1, 2, 3)
        println(s"Elements are = $bs")
          
        // Adding elements in BitSet
        val bs1: BitSet = bs ++ BitSet(4, 5, 6)
        println(s"Add more than one elements to BitSet = $bs1")
    }
}
输出:
Initialize a BitSet
Elements are = BitSet(0, 1, 2, 3)
Add more than one elements to BitSet  = BitSet(0, 1, 2, 3, 4, 5, 6)


移除 BitSet 中的元素:我们可以使用 - 符号移除 BitSet 中的元素。下面是在 BitSet 中删除元素的示例。

例子 :

// Scala program of removing element in BitSet
import scala.collection.immutable.BitSet
  
// Creating object
object GFG
{ 
    // Main method
    def main(args:Array[String])
    { 
        println("Initialize a BitSet")
          
        // Creating BitSet
        val bs: BitSet = BitSet(0, 1, 2, 3)
        println(s"Elements are = $bs")
          
        // removing elements in BitSet
        val bs1: BitSet = bs - 2
        println(s"remove element from bitset = $bs1")
    }
}
输出:
Initialize a BitSet
Elements are = BitSet(0, 1, 2, 3)
remove element from bitset = BitSet(0, 1, 3)


查找两个 BitSet 之间的交集:我们可以使用 & 符号找到两个 BitSet 之间的交集。下面是在两个 BitSet 之间查找交集的示例。

例子 :

// Scala program of finding the intersection between two BitSets
import scala.collection.immutable.BitSet
  
// Creating object
object GFG
{ 
    // Main method
    def main(args:Array[String])
    { 
        println("Initialize two BitSets")
          
        // Creating two BitSet
        val bs: BitSet = BitSet(0, 1, 2, 3)
        println(s"Elements of bitset1 are = $bs")
          
        val bs1: BitSet = BitSet(4, 5, 3, 6)
        println(s"Elements of bitset2 are = $bs1")
          
        // finding the intersection between two BitSets
        println(s"Intersection of bitSet1 and bitSet2 = ${bs & bs1}")
    }
}
输出:
Initialize two BitSets
Elements of bitset1 are = BitSet(0, 1, 2, 3)
Elements of bitset2 are = BitSet(3, 4, 5, 6)
Intersection of bitSet1 and bitSet2 = BitSet(3)


初始化一个空的 BitSet :

例子 :

// Scala program of Initializing an empty BitSet
import scala.collection.immutable.BitSet
  
// Creating object
object GFG
{ 
    // Main method
    def main(args:Array[String])
    { 
        // Initializing an empty BitSet
        val emptyBitSet: BitSet = BitSet.empty
        println(s"Empty BitSet = $emptyBitSet")
    }
}
输出:
Empty BitSet = BitSet()