Scala中的HashSet

Scala HashSet (Scala HashSet)

A HashSet is a special type of collection that is unordered i.e. the order of inserted elements is not defined. For storing its elements and keeping track of them it uses hashmaps.

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

In Scala programming language, HashSet class extents immutable set along with abstractSet trait. The element in HashSet is stored using hashcode.

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

Syntax:

句法:

    var hashset_name = HashSet(elements...)

Now, as we know the basics of HashSet in scala lets create one and perform some operations on it.

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

在Scala中创建HashSet (Creating a HashSet in Scala)

Creating a HashSet in the Scala programming language is quite an easy process. The below code shows how to,

用Scala编程语言创建HashSet相当容易。 以下代码显示了如何

1) Program to show the implementation creation of 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") 
	} 
} 

Output

输出量

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

在Scala中从HashSet查找元素 (Finding an element from HashSet in Scala)

Searching for an element from HashSet in Scala is a direct process. HashSet in scala will return a boolean value (True or False) for element found or not.

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

2) Program to show the implementation element search in hashset

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)})
	} 
} 

Output

输出量

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中添加元素 (Adding elements in HashSet)

a) Adding Single element

a)添加单个元素

In a HashSet, addition operation is also valid. The + operator is used to add the new element in scala HashSet. As HashSet is an immutable addition will be implemented in a new HashSet.

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

Program to show the implementation of adding elements in 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")
	} 
} 

Output

输出量

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) Adding multiple elements in HashSet

b)在HashSet中添加多个元素

The ++ operator is used to add more than one element in HashSet.

++运算符用于在HashSet中添加多个元素。

Program to show the implementation of adding elements in 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")
	} 
} 

Output

输出量

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中删除元素 (Removing elements from HashSet in Scala)

Elements of HashSet can be removed by using the - operator in Scala. Again you need to create a new HashSet to assign the operated HashSet.

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

1) program to show the implementation of removing elements in 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")
	} 
} 

Output

输出量

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

查找两个散列集中的共同元素(交叉操作) (Find the elements that are in common in Two HashSets (Intersection operation))

In Scala common elements between two HashSet using the & sign, this will be given common elements in both the HashSets.

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

Program to show the implementation of intersection of 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})
	} 
} 

Output

输出量

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 (Creating an Empty HashSet in Scala)

In Scala, we can create an Empty HashSet with zero elements.

在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)
	} 
} 

Output

输出量

An empty HashSet : HashSet()


翻译自: https://www.includehelp.com/scala/hashset.aspx

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值