📜  Scala中的HashSet

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

Scala中的HashSet

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

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",
                                        "GeeksForGeeks", "Author")
            println(s"Elements are = $hashSet")
        }
    } 
    

    输出:

    Initialize a HashSet
    Elements are = Set(Geeks, Author, GeeksForGeeks)
  • 检查 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",
                                    "GeeksForGeeks", "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, GeeksForGeeks)
    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",
                                "GeeksForGeeks", "Author")
            println(s"Elements are = $hs")
              
            // Adding an element in HashSet
            val hs1: HashSet[String] = hs + "GeeksClasses"
            println(s"Adding elements to HashSet = $hs1")
        }
    }
    

    输出:

    Initialize a HashSet
    Elements are = Set(Geeks, Author, GeeksForGeeks)
    Adding elements to HashSet = Set(GeeksClasses, Geeks, Author, GeeksForGeeks)
    
  • 在 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",
                                    "GeeksForGeeks", "Author")
            println(s"Elements are = $hs")
              
            // Adding elements in HashSet
            val hs1: HashSet[String] = hs ++ HashSet[String]("Java", "Scala")
            println(s"Add more than one HashSets = $hs1")
        }
    }
    

    输出:

    Initialize a HashSet
    Elements are = Set(Geeks, Author, GeeksForGeeks)
    Add more than one HashSets = Set(Scala, Geeks, Author, Java, GeeksForGeeks)
    
  • 移除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",
                                    "GeeksForGeeks", "Author")
            println(s"Elements are = $hs")
              
            // removing elements in HashSet
            val hs1: HashSet[String] = hs - "Geeks"
            println(s"remove element from hashset = $hs1")
        }
    }
    

    输出:

    Initialize a HashSet
    Elements are = Set(Geeks, Author, GeeksForGeeks)
    remove element from hashset = Set(Author, GeeksForGeeks)
  • 查找两个 HashSet 之间的交集:我们可以使用 & 符号找到两个 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",
                                "GeeksForGeeks", "Author")
            println(s"Elements of hashset1 are = $hs")
              
            val hs1: 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, GeeksForGeeks)
    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()