Scala 中的 ListSet
集合是仅包含不可重复的唯一项目的集合,列表是包含不可变数据的集合。在 Scala 中, ListSet class
使用基于列表的数据结构实现不可变集合。元素以相反的插入顺序存储,这意味着最新的元素位于列表的头部。它维护插入顺序。
Listset 仅用于少数元素。我们可以通过调用构造函数或应用函数ListSet.empty
。它的迭代和遍历方法以它们首次插入的相同顺序访问元素。
句法:
var ListSetName = ListSet(element1, element2, element3, ....)
ListSet 操作
初始化 ListSet :下面是创建或初始化 ListSet 的示例。
// Scala program to Initialize a ListSet
import scala.collection.immutable._
// Creating object
object GFG
{
// Main method
def main(args:Array[String])
{
println("Initializing an immutable ListSet ")
// Creating ListSet
val listSet1: ListSet[String] = ListSet("GeeksForGeeks",
"Article", "Scala")
println(s"Elements of listSet1 = $listSet1")
}
}
输出:
Initializing an immutable ListSet
Elements of listSet1 = ListSet(Scala, Article, GeeksForGeeks)
检查 ListSet 中的特定元素:
// Scala program to Check specific elements in ListSet
import scala.collection.immutable._
// Creating object
object GFG
{
// Main method
def main(args:Array[String])
{
println("Initializing an immutable ListSet ")
// Creating ListSet
val listSet1: ListSet[String] = ListSet("GeeksForGeeks",
"Article", "Scala")
println(s"Elements of listSet1 = $listSet1")
println("Check elements of immutable ListSet")
// Checking element in a ListSet
println(s"GeeksForGeeks = ${listSet1("GeeksForGeeks")}")
println(s"Student = ${listSet1("Student")}")
println(s"Scala = ${listSet1("Scala")}")
}
}
输出:
Initializing an immutable ListSet
Elements of listSet1 = ListSet(Scala, Article, GeeksForGeeks)
Check elements of immutable ListSet
GeeksForGeeks = true
Student = false
Scala = true
在 ListSet 中添加元素:我们可以使用 +运算符在 ListSet 中添加元素。下面是在 ListSet 中添加元素的示例。
// Scala program to Add an element in a ListSet
import scala.collection.immutable._
// Creating object
object GFG
{
// Main method
def main(args:Array[String])
{
println("Initializing an immutable ListSet ")
// Creating ListSet
val listSet1: ListSet[String] = ListSet("GeeksForGeeks",
"Article", "Scala")
println(s"Elements of listSet1 = $listSet1")
// Adding element in ListSet
println("Add element of immutable ListSet ")
val listSet2: ListSet[String] = listSet1 + "Java"
println(s"Adding element java to ListSet $listSet2")
}
}
输出:
Initializing an immutable ListSet
Elements of listSet1 = ListSet(Scala, Article, GeeksForGeeks)
Add element of immutable ListSet
Adding element java to ListSet ListSet(Java, Scala, Article, GeeksForGeeks)
添加两个ListSet:我们可以使用++运算符添加两个ListSet。下面是添加两个 ListSet 的示例。
// Scala program to Add two ListSet
import scala.collection.immutable._
// Creating object
object GFG
{
// Main method
def main(args:Array[String])
{
println("Initializing an immutable ListSet ")
// Creating ListSet
val listSet1: ListSet[String] = ListSet("GeeksForGeeks",
"Article", "Scala")
println(s"Elements of listSet1 = $listSet1")
// Adding two ListSet
val listSet2: ListSet[String] = listSet1 ++ ListSet("Java",
"Csharp")
println(s"After adding two lists $listSet2")
}
}
输出:
Initializing an immutable ListSet
Elements of listSet1 = ListSet(Scala, Article, GeeksForGeeks)
After adding two lists ListSet(Java, Csharp, Scala, Article, GeeksForGeeks)
从 ListSet 中删除元素:我们可以使用 –运算符删除 ListSet 中的元素。下面是在 ListSet 中删除元素的示例。
// Scala program to Remove element from the ListSet
import scala.collection.immutable._
// Creating object
object GFG
{
// Main method
def main(args:Array[String])
{
println("Initializing an immutable ListSet ")
// Creating ListSet
val listSet1: ListSet[String] = ListSet("GeeksForGeeks",
"Article", "Scala")
println(s"Elements of listSet1 = $listSet1")
println("Remove element from the ListSet ")
val listSet2: ListSet[String] = listSet1 - ("Article")
println(s"After removing element from listset = $listSet2")
}
}
输出:
Initializing an immutable ListSet
Elements of listSet1 = ListSet(Scala, Article, GeeksForGeeks)
Remove element from the ListSet
After removing element from listset = ListSet(Scala, GeeksForGeeks)
初始化一个空的 ListSet :
// Scala program to print empty ListSet
import scala.collection.immutable._
// Creating object
object GFG
{
// Main method
def main(args:Array[String])
{
// Creating an empty ListSet
println("Initialize an empty ListSet")
val emptyListSet: ListSet[String] = ListSet.empty[String]
println(s"String type empty ListSet = $emptyListSet")
}
}
输出:
Initialize an empty ListSet
String type empty ListSet = ListSet()
注意:我们可以通过应用函数ListSet.empty
或调用构造函数来创建空 ListSet。