数据结构:在Scala中的HashSet用法指南

2021年3月12日12:58:21 发表评论 1,558 次浏览

HashSet

是密封类。它扩展了不可变的Set和AbstractSet特征。哈希码用于存储元素。它既不对元素排序也不维护插入顺序。由HashSet类实现的Set接口, 由哈希表支持。在Scala中, 已知的HashSet是Set语义的具体实现。

语法如下:

var HashsetName = HashSet(element1, element2, element3, ....)

使用HashSet执行操作

初始化HashSet:

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

范例:

// Scala program of Initializing HashSet
import scala.collection.immutable.HashSet
  
// Creating object
object GFG
{ 
     // Main method
     def main(args : Array[String])
     { 
         println( "Initialize a HashSet" )
          
         // Creating HashSet
         val hashSet : HashSet[String] = HashSet( "Geeks" , "lsbin" , "Author" )
         println(s "Elements are = $hashSet" )
     }
}

输出如下:

Initialize a HashSet
Elements are = Set(Geeks, Author, lsbin)

检查HashSet中的特定元素:

范例:

// Scala program of Check specific elements in HashSet
import scala.collection.immutable.HashSet
  
// Creating object
object GFG
{ 
     // Main method
     def main(args : Array[String])
     { 
         println( "Initialize a HashSet" )
          
         // Creating HashSet
         val hashSet : HashSet[String] = HashSet( "Geeks" , "lsbin" , "Author" )
         println(s "Elements are = $hashSet" )
          
         // Checking
         println(s "Element Geeks = ${hashSet(" Geeks ")}" )
         println(s "Element Student = ${hashSet(" Student ")}" )
     }
}

输出如下:

Initialize a HashSet
Elements are = Set(Geeks, Author, lsbin)
Element Geeks = true
Element Student = false

在HashSet中添加元素:

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

范例:

// Scala program of adding an element in HashSet
import scala.collection.immutable.HashSet
  
// Creating object
object GFG
{ 
     // Main method
     def main(args : Array[String])
     { 
         println( "Initialize a HashSet" )
          
         // Creating HashSet
         val hs : HashSet[String] = HashSet( "Geeks" , "lsbin" , "Author" )
         println(s "Elements are = $hs" )
          
         // Adding an element in HashSet
         val hs 1 : HashSet[String] = hs + "GeeksClasses"
         println(s "Adding elements to HashSet = $hs1" )
     }
}

输出如下:

Initialize a HashSet
Elements are = Set(Geeks, Author, lsbin)
Adding elements to HashSet = Set(GeeksClasses, Geeks, Author, lsbin)

在HashSet中添加多个元素:

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

范例:

// Scala program of adding more elements in HashSet
import scala.collection.immutable.HashSet
  
// Creating object
object GFG
{ 
     // Main method
     def main(args : Array[String])
     { 
         println( "Initialize a HashSet" )
          
         // Creating HashSet
         val hs : HashSet[String] = HashSet( "Geeks" , "lsbin" , "Author" )
         println(s "Elements are = $hs" )
          
         // Adding elements in HashSet
         val hs 1 : HashSet[String] = hs ++ HashSet[String]( "Java" , "Scala" )
         println(s "Add more than one HashSets = $hs1" )
     }
}

输出如下:

Initialize a HashSet
Elements are = Set(Geeks, Author, lsbin)
Add more than one HashSets = Set(Scala, Geeks, Author, Java, lsbin)

删除HashSet中的元素:

我们可以使用–符号删除HashSet中的元素。以下是在HashSet中删除元素的示例。

范例:

// Scala program of removing element in HashSet
import scala.collection.immutable.HashSet
  
// Creating object
object GFG
{ 
     // Main method
     def main(args : Array[String])
     { 
         println( "Initialize a HashSet" )
          
         // Creating HashSet
         val hs : HashSet[String] = HashSet( "Geeks" , "lsbin" , "Author" )
         println(s "Elements are = $hs" )
          
         // removing elements in HashSet
         val hs 1 : HashSet[String] = hs - "Geeks"
         println(s "remove element from hashset = $hs1" )
     }
}

输出如下:

Initialize a HashSet
Elements are = Set(Geeks, Author, lsbin)
remove element from hashset = Set(Author, lsbin)

找到两个HashSets之间的交集:

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

范例:

// Scala program of finding the intersection between two HashSets
import scala.collection.immutable.HashSet
  
// Creating object
object GFG
{ 
     // Main method
     def main(args : Array[String])
     { 
         println( "Initialize two HashSets" )
          
         // Creating two HashSet
         val hs : HashSet[String] = HashSet( "Geeks" , "lsbin" , "Author" )
         println(s "Elements of hashset1 are = $hs" )
          
         val hs 1 : HashSet[String] = HashSet( "Java" , "Geeks" , "Scala" )
         println(s "Elements of hashset2 are = $hs1" )
          
         // finding the intersection between two HashSets
         println(s "Intersection of hashSet1 and hashSet2 = ${hs & hs1}" )
     }
}

输出如下:

Initialize two HashSets
Elements of hashset1  are = Set(Geeks, Author, lsbin)
Elements of hashset2 are = Set(Scala, Geeks, Java)
Intersection of hashSet1 and hashSet2 = Set(Geeks)

初始化一个空的HashSet:

范例:

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

输出如下:

Empty HashSet = Set()

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: