Scala中的HashSet

Scala HashSet

HashSet的是一种特殊类型的集合即无序即没有定义插入的元素的顺序。为了存储其元素并跟踪它们,它使用哈希映射。

在Scala编程语言中,HashSet类扩展了不可变集合以及abstractSet特征。HashSet中的元素使用哈希码存储。

语法:

    var hashset_name = HashSet(elements...)

现在,据我们所知,scala中HashSet基础知识让我们创建一个并对其执行一些操作。

在Scala中创建HashSet

用Scala编程语言创建HashSet是一个非常简单的过程。以下代码显示了如何

1)演示实现哈希集创建的程序

import scala.collection.immutable.HashSet 
object myObject
{ 
	def main(args:Array[String]) 
	{ 
		val hashSet: HashSet[Int] = HashSet(23, 53, 89, 01, 90) 
		println(s"The elements of the HashSet are = $hashSet") 
	} 
}

输出结果

The elements of the HashSet are = HashSet(53, 23, 90, 89, 1)

在Scala中从HashSet查找元素

从Scala中的HashSet搜索元素是一个直接的过程。Scala中的HashSet将为是否找到元素返回布尔值(True或False)。

2)在哈希集中显示实现元素搜索的程序

import scala.collection.immutable.HashSet 
object myObject
{ 
	def main(args:Array[String]) 
	{ 
		val hashSet: HashSet[Int] = HashSet(23, 53, 89, 01, 90) 
		println(s"The elements of the HashSet are = $hashSet") 
		println("Number 43 is in HashSet? "+ {hashSet(43)})
		println("Number 90 is in HashSet? "+ {hashSet(90)})
	} 
}

输出结果

The elements of the HashSet are = HashSet(53, 23, 90, 89, 1)
Number 43 is in HashSet? false
Number 90 is in HashSet? true

在HashSet中添加元素

a)添加单个元素

在HashSet中,加法运算也有效。+运算符用于在scala HashSet中添加新元素。由于HashSet是不变的,因此将在新的HashSet中实现。

该程序显示在哈希集中添加元素的实现

import scala.collection.immutable.HashSet 
object myObject
{ 
	def main(args:Array[String]) 
	{ 
		val hashSet: HashSet[Int] = HashSet(23, 53, 89, 01, 90) 
		println(s"The elements of the HashSet are = $hashSet") 
		val hashSet2 : HashSet[Int] = hashSet + 45
		println(s"The elements of new HashSet are = $hashSet2")
	} 
}

输出结果

The elements of the HashSet are = HashSet(53, 23, 90, 89, 1)
The elements of new HashSet are = HashSet(53, 45, 23, 90, 89, 1)

b)在HashSet中添加多个元素

++操作者使用一个以上的元件中的HashSet添加。

该程序显示在哈希集中添加元素的实现

import scala.collection.immutable.HashSet 
object myObject
{ 
	def main(args:Array[String]) 
	{ 
		val hashSet: HashSet[Int] = HashSet(23, 53, 89, 01, 90) 
		println(s"The elements of the HashSet are = $hashSet") 
		val hashSet2 : HashSet[Int] = hashSet ++ HashSet[Int](45, 31)
		println(s"The elements of new HashSet are = $hashSet2")
	} 
}

输出结果

The elements of the HashSet are = HashSet(53, 23, 90, 89, 1)
The elements of new HashSet are = HashSet(53, 45, 31, 23, 90, 89, 1)

在Scala中从HashSet中删除元素

可以使用Scala中的-运算符删除HashSet的元素。同样,您需要创建一个新的HashSet来分配操作的HashSet。

1)程序显示删除哈希集中的元素的实现

import scala.collection.immutable.HashSet 
object myObject
{ 
	def main(args:Array[String]) 
	{ 
		val hashSet: HashSet[Int] = HashSet(23, 53, 89, 01, 90) 
		println(s"The elements of the HashSet are = $hashSet") 
		val hashSet2 : HashSet[Int] = hashSet - 53
		println(s"The elements of new HashSet are = $hashSet2")
	} 
}

输出结果

The elements of the HashSet are = HashSet(53, 23, 90, 89, 1)
The elements of new HashSet are = HashSet(23, 90, 89, 1)

查找两个散列集中的共同元素(交叉操作)

在使用&符号的两个HashSet之间的Scala公共元素中,这将在两个HashSet中都被赋予公共元素。

程序展示哈希集交集的实现

import scala.collection.immutable.HashSet 
object myObject
{ 
	def main(args:Array[String]) 
	{ 
		val hashSet: HashSet[Int] = HashSet(23, 53, 89, 01, 90) 
		println(s"The elements of the HashSet1 are = $hashSet") 
		val hashSet2 : HashSet[Int] = HashSet(5, 23, 45, 01, 31, 90)
		println(s"The elements of new HashSet2 are = $hashSet2")
		println("The intersection of HashSet1 and HashSet2 are " + {hashSet & hashSet2})
	} 
}

输出结果

The elements of the HashSet1 are = HashSet(53, 23, 90, 89, 1)
The elements of new HashSet2 are = HashSet(5, 1, 45, 31, 23, 90)
The intersection of HashSet1 and HashSet2 are HashSet(1, 23, 90)

在Scala中创建一个空HashSet

在Scala中,我们可以创建一个包含零个元素的Empty HashSet。

import scala.collection.immutable.HashSet 
object myObject
{ 
	def main(args:Array[String]) 
	{ 
		val hashSet: HashSet[Int] = HashSet.empty[Int]
		println("An empty HashSet : "+ hashSet)
	} 
}

输出结果

An empty HashSet : HashSet()