Scala中的ListSet用法详细介绍

2021年3月15日08:34:21 发表评论 840 次浏览

一种组是仅包含不可重复的唯一项目的集合, 列表是包含不可变数据的集合。在斯卡拉, ListSet类使用list基于数据的结构。元素以相反的插入顺序存储, 这意味着最新的元素位于列表的开头。它保持插入顺序。

Listset仅用于少量元素。我们可以通过调用构造函数或应用函数来创建空的ListSetListSet.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 listSet 1 : ListSet[String] = ListSet( "lsbin" , "Article" , "Scala" )
         println(s "Elements of listSet1 = $listSet1" )
     } 
}

输出如下:

Initializing an immutable ListSet 
Elements of listSet1 = ListSet(Scala, Article, lsbin)

检查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 listSet 1 : ListSet[String] = ListSet( "lsbin" , "Article" , "Scala" )
         println(s "Elements of listSet1 = $listSet1" )
          
         println( "Check elements of immutable ListSet" )
          
         // Checking element in a ListSet
         println(s "lsbin = ${listSet1(" lsbin ")}" )
         println(s "Student = ${listSet1(" Student ")}" )
         println(s "Scala = ${listSet1(" Scala ")}" )
     } 
}

输出如下:

Initializing an immutable ListSet 
Elements of listSet1 = ListSet(Scala, Article, lsbin)
Check elements of immutable ListSet
lsbin = 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 listSet 1 : ListSet[String] = ListSet( "lsbin" , "Article" , "Scala" )
         println(s "Elements of listSet1 = $listSet1" )
          
         // Adding element in ListSet
         println( "Add element of immutable ListSet " )
         val listSet 2 : ListSet[String] = listSet 1 + "Java"
         println(s "Adding element java to ListSet $listSet2" )
     } 
}

输出如下:

Initializing an immutable ListSet 
Elements of listSet1 = ListSet(Scala, Article, lsbin)
Add element of immutable ListSet 
Adding element java to ListSet ListSet(Java, Scala, Article, lsbin)

添加两个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 listSet 1 : ListSet[String] = ListSet( "lsbin" , "Article" , "Scala" )
         println(s "Elements of listSet1 = $listSet1" )
          
         // Adding two ListSet
         val listSet 2 : ListSet[String] = listSet 1 ++ ListSet( "Java" , "Csharp" )
         println(s "After adding two lists $listSet2" )
          
  
     } 
}

输出如下:

Initializing an immutable ListSet 
Elements of listSet1 = ListSet(Scala, Article, lsbin)
After adding two lists ListSet(Java, Csharp, Scala, Article, lsbin)

从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 listSet 1 : ListSet[String] = ListSet( "lsbin" , "Article" , "Scala" )
         println(s "Elements of listSet1 = $listSet1" )
          
         println( "Remove element from the ListSet " )
         val listSet 2 : ListSet[String] = listSet 1 - ( "Article" )
         println(s "After removing element from listset = $listSet2" )
          
  
     } 
}

输出如下:

Initializing an immutable ListSet 
Elements of listSet1 = ListSet(Scala, Article, lsbin)
Remove element from the ListSet 
After removing element from listset = ListSet(Scala, lsbin)

初始化一个空的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()

注意:我们可以通过应用函数来创建空的ListSetListSet.empty或通过调用构造函数。


木子山

发表评论

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