📜  Scala Trait 可遍历 |第 2 组

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

Scala Trait 可遍历 |第 2 组

先决条件- Scala Trait Traversable |第一组
在之前的 Set 中,我们已经看到了 Class Taversable 执行的一些操作。现在,在这个 Set 中,我们将感知更多的操作。
这些操作如下:

  • 转换操作:
    转换操作是toList、toSeq、toArray、toStream、toSet、toMap、toIterable 和 toIndexedSeq 。这些操作将 Traversable 的 Collection 变成了一个相对不同的东西。
    例子 :
Scala
// Scala program of Conversion operation
 
// Creating object
object Conversion
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating Set of numbers
        val q = Set(2, 7, 8, 15, 19)
 
        // Converting Set to an Array
        val r = q.toArray
 
        // Displaying an Array
        println(r)
    }
}


Scala
// Scala program of Conversion operation
 
// Creating object
object Conversion
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating Set of numbers
        val q = Set(2, 6, 3, 7, 15, 20)
 
        // Converting a Set to a List
        val r = q.toList
 
        // Displaying a List
        println(r)
         
    }
}


Scala
// Scala program of Conversion operation
 
// Creating object
object Conversion
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating List of numbers
        val x = List(9, 10, 13, 15, 18, 19)
 
        // Converting a List to a Set
        val y = x.toSet
 
        // Displaying a Set
        println(y)
    }
}


Scala
// Scala program of Conversion operation
 
// Creating object
object Conversion
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating Set of numbers
        val m = Set(2, 4, 6, 8, 11, 15)
 
        // Converting a Set to a Sequence
        val n = m.toSeq
 
        // Displaying a Sequence
        println(n)
    }
}


Scala
// Scala program of Conversion operation
 
// Creating object
object Conversion
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating Set of numbers
        val x = Set(8, 10, 13, 15, 18)
 
        // Converting a Set to an Iterable
        val y = x.toIterable
 
        // Displaying an iterable
        println(y)
    }
}


Scala
// Scala program of Conversion operation
 
// Creating object
object Conversion
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating Set of numbers
        val x = Set(2, 4, 5, 6, 7, 9)
 
        // Converting a Set to an
        // Indexed sequence
        val y = x.toIndexedSeq
 
        // Displaying an Indexed sequence
        println(y)
    }
}


Scala
// Scala program of Conversion operation
 
// Creating object
object Conversion
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating Set of numbers
        val x = Set(15, 17, 18, 19, 22, 25)
 
        // Converting a Set to a stream
        val y = x.toStream
 
        // Displaying a Stream
        println(y)
    }
}


Scala
// Scala program of Conversion operation
 
// Creating object
object Conversion
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating a Set of parameters
        val x = Set("GfG" -> "CS portal", "Nidhi" -> "a Geek")
 
        // Converting a Set to a Map
        val y = x.toMap
 
        // Displaying a Map
        println(y)
    }
}


Scala
// Scala program of Size info operation
 
// Creating object
object Sizeinfo
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating a map
        val x = Map("gfg" -> "cs", "nidhi" -> "geek")
 
        // Applying Size info operation
        val y = x.isEmpty
 
        // Displays true if map is
        // empty
        println(y)
    }
}


Scala
// Scala program of Size info operation
 
// Creating object
object Sizeinfo
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating a map
        val x = Map("gfg" -> "cs", "nidhi" -> "geek")
 
        // Applying Size info operation
        val y = x.nonEmpty
 
        // Displays true if map is
        // not empty
        println(y)
    }
}


Scala
// Scala program of Size info operation
 
// Creating object
object Sizeinfo
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating a map
        val q = Map("gfg" -> "cs", "nidhi" -> "geek",
                                    "geeta" -> "coder")
 
        // Applying Size info operation
        val r = q.size
 
        // Displays size of the Map
        println(r)
    }
}


Scala
// Scala program of Size info operation
 
// Creating object
object Sizeinfo
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating a map
        val q = Map("gfg" -> "cs", "nidhi" -> "geek",
                                    "geeta" -> "coder")
 
        // Applying Size info operation
        val r = q.hasDefiniteSize
 
        // Displays true if number of
        // elements in Map are finite
        println(r)
    }
}


Scala
// Scala program of Element
// retrieval operation
 
// Creating object
object Retrieval
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating a Set of numbers
        val x = Set(12, 13, 14, 15)
 
        // Applying element retrieval
        // operation
        val y = x.lastOption
 
        // Displays last element
        // of the Set
        println(y)
    }
}


Scala
// Scala program of Element
// retrieval operation
 
// Creating object
object Retrieval
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating a Set of numbers
        val x = Set(12, 13, 14, 15)
 
        // Applying element retrieval
        // operation
        val y = x.last
 
        // Displays last element
        // of the Set
        println(y)
    }
}


Scala
// Scala program of Element
// retrieval operation
 
// Creating object
object Retrieval
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating a List of numbers
        val q = List(12, 24, 36, 48)
 
        // Applying element retrieval
        // operation
        val r = q.head
 
        // Displays first element
        // of the List
        println(r)
    }
}


Scala
// Scala program of Element
// retrieval operation
 
// Creating object
object Retrieval
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating a List of numbers
        val x = List(8, 10, 21, 17, 29)
 
        // Applying element retrieval
        // operation
        val y = x.find(_ % 3 == 0)
 
        // Displays first element
        // matching the condition
        println(y)
 
    }
}


Scala
// Scala program of Element
// retrieval operation
 
// Creating object
object Retrieval
{
 
    // Main method
    def main(args: Array[String])
    {
     
        // Creating List of numbers
        val p = List(7, 9, 11, 19, 21)
 
        // Creating a empty List
        val q = List()
 
        // Applying element retrieval
        // operation
        val r = p.headOption
        val s = q.headOption
 
        // Displays first element
        // if the List is not empty
        println(r)
        println(s)
    }
}


输出:
[I@506e1b77

  • 在这里,Conversion 操作,即toArray会将上述 Set(或任何 Traversable)转换为 Array。
    例子 :

斯卡拉

// Scala program of Conversion operation
 
// Creating object
object Conversion
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating Set of numbers
        val q = Set(2, 6, 3, 7, 15, 20)
 
        // Converting a Set to a List
        val r = q.toList
 
        // Displaying a List
        println(r)
         
    }
}
输出:
List(20, 6, 2, 7, 3, 15)

  • 在这里, toList会将任何 Traversable 的 Collection 转换为 List。
    例子 :

斯卡拉

// Scala program of Conversion operation
 
// Creating object
object Conversion
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating List of numbers
        val x = List(9, 10, 13, 15, 18, 19)
 
        // Converting a List to a Set
        val y = x.toSet
 
        // Displaying a Set
        println(y)
    }
}
输出:
Set(10, 9, 13, 18, 19, 15)

  • 在这里,Conversion 操作,即toSet会将任何 Traversable 集合转换为 Set。
    例子 :

斯卡拉

// Scala program of Conversion operation
 
// Creating object
object Conversion
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating Set of numbers
        val m = Set(2, 4, 6, 8, 11, 15)
 
        // Converting a Set to a Sequence
        val n = m.toSeq
 
        // Displaying a Sequence
        println(n)
    }
}
输出:
ArrayBuffer(6, 2, 11, 8, 4, 15)

  • 在这里,转换操作,即toSeq将任何 Traversable 集合转换为序列。此处生成的序列在 Vectors 中使用。
    例子 :

斯卡拉

// Scala program of Conversion operation
 
// Creating object
object Conversion
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating Set of numbers
        val x = Set(8, 10, 13, 15, 18)
 
        // Converting a Set to an Iterable
        val y = x.toIterable
 
        // Displaying an iterable
        println(y)
    }
}
输出:
Set(10, 13, 18, 8, 15)

  • 在这里,转换操作,即toIterable (它迭代集合的所有元素)会将 Traversable 的任何集合转换为 Iterable。
    例子 :

斯卡拉

// Scala program of Conversion operation
 
// Creating object
object Conversion
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating Set of numbers
        val x = Set(2, 4, 5, 6, 7, 9)
 
        // Converting a Set to an
        // Indexed sequence
        val y = x.toIndexedSeq
 
        // Displaying an Indexed sequence
        println(y)
    }
}
输出:
Vector(5, 6, 9, 2, 7, 4)

  • 这里,转换操作,即toIndexedSeq将任何 Traversable 转换为 Indexed 序列。此处生成的索引序列用于字符串和向量。
    例子 :

斯卡拉

// Scala program of Conversion operation
 
// Creating object
object Conversion
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating Set of numbers
        val x = Set(15, 17, 18, 19, 22, 25)
 
        // Converting a Set to a stream
        val y = x.toStream
 
        // Displaying a Stream
        println(y)
    }
}
输出:
Stream(25, ?)

  • 在这里,Conversion 操作,即toStream将任何 Traversable 集合转换为 Stream。这个 Stream 是惰性枚举的。
    例子 :

斯卡拉

// Scala program of Conversion operation
 
// Creating object
object Conversion
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating a Set of parameters
        val x = Set("GfG" -> "CS portal", "Nidhi" -> "a Geek")
 
        // Converting a Set to a Map
        val y = x.toMap
 
        // Displaying a Map
        println(y)
    }
}
输出:
Map(GfG -> CS portal, Nidhi -> a Geek)

  • 在这里, toMap会将任何 Traversable 转换为 Map。 Set 或 List 必须有参数。
  • 尺寸信息操作:
    Size 信息操作是nonEmpty、isEmpty、hasDefiniteSize 和 size 。这些操作可以指定给定的操作是有限的还是无限的。
    例子 :

斯卡拉

// Scala program of Size info operation
 
// Creating object
object Sizeinfo
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating a map
        val x = Map("gfg" -> "cs", "nidhi" -> "geek")
 
        // Applying Size info operation
        val y = x.isEmpty
 
        // Displays true if map is
        // empty
        println(y)
    }
}
输出:
false

  • 在这里, isEmpty检查 Traversable 集合是否为空。如果元素集合为空,则打印 true,如果不为空,则打印 false。
    例子 :

斯卡拉

// Scala program of Size info operation
 
// Creating object
object Sizeinfo
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating a map
        val x = Map("gfg" -> "cs", "nidhi" -> "geek")
 
        // Applying Size info operation
        val y = x.nonEmpty
 
        // Displays true if map is
        // not empty
        println(y)
    }
}
输出:
true

  • 这里, nonEmpty检查 Traversable 集合是否包含元素。如果集合中有元素,则显示 true 否则为 false。
    例子 :

斯卡拉

// Scala program of Size info operation
 
// Creating object
object Sizeinfo
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating a map
        val q = Map("gfg" -> "cs", "nidhi" -> "geek",
                                    "geeta" -> "coder")
 
        // Applying Size info operation
        val r = q.size
 
        // Displays size of the Map
        println(r)
    }
}
输出:
3

  • 这里使用size来评估 Traversable 集合的大小。
    例子 :

斯卡拉

// Scala program of Size info operation
 
// Creating object
object Sizeinfo
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating a map
        val q = Map("gfg" -> "cs", "nidhi" -> "geek",
                                    "geeta" -> "coder")
 
        // Applying Size info operation
        val r = q.hasDefiniteSize
 
        // Displays true if number of
        // elements in Map are finite
        println(r)
    }
}
输出:
true

  • 在这里, hasDefiniteSize用于检查 Traversable 集合是否具有有限元素。如果集合是有限的,则返回 true,否则返回 false。
  • 元素检索操作:
    Element检索操作包括last、head、lastOption、headOption和find 。这些操作用于检索 Traversable 集合的第一个或最后一个元素,或检索与给定条件对应的第一个元素。
    例子 :

斯卡拉

// Scala program of Element
// retrieval operation
 
// Creating object
object Retrieval
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating a Set of numbers
        val x = Set(12, 13, 14, 15)
 
        // Applying element retrieval
        // operation
        val y = x.lastOption
 
        // Displays last element
        // of the Set
        println(y)
    }
}
输出:
Some(15)

  • 在这里, Traversable 的最后一个元素由lastOption返回。声明的集合必须是有序的,如果集合中没有元素,则返回 None。
    例子 :

斯卡拉

// Scala program of Element
// retrieval operation
 
// Creating object
object Retrieval
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating a Set of numbers
        val x = Set(12, 13, 14, 15)
 
        // Applying element retrieval
        // operation
        val y = x.last
 
        // Displays last element
        // of the Set
        println(y)
    }
}
输出:
15

  • 在这里, last将返回指定集合的最后一个元素。集合必须是有序的,如果它不是有序的,则返回一些随机元素。
    例子 :

斯卡拉

// Scala program of Element
// retrieval operation
 
// Creating object
object Retrieval
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating a List of numbers
        val q = List(12, 24, 36, 48)
 
        // Applying element retrieval
        // operation
        val r = q.head
 
        // Displays first element
        // of the List
        println(r)
    }
}
输出:
12

  • 在这里,如果它是有序的, head将返回 Traversable 集合的第一个元素,如果该集合不是有序的,则返回任何随机元素。
    例子:

斯卡拉

// Scala program of Element
// retrieval operation
 
// Creating object
object Retrieval
{
 
    // Main method
    def main(args: Array[String])
    {
 
        // Creating a List of numbers
        val x = List(8, 10, 21, 17, 29)
 
        // Applying element retrieval
        // operation
        val y = x.find(_ % 3 == 0)
 
        // Displays first element
        // matching the condition
        println(y)
 
    }
}
输出:
Some(21)

  • 在这里, find将检索集合的第一个元素,它与所述条件匹配。
    例子 :

斯卡拉

// Scala program of Element
// retrieval operation
 
// Creating object
object Retrieval
{
 
    // Main method
    def main(args: Array[String])
    {
     
        // Creating List of numbers
        val p = List(7, 9, 11, 19, 21)
 
        // Creating a empty List
        val q = List()
 
        // Applying element retrieval
        // operation
        val r = p.headOption
        val s = q.headOption
 
        // Displays first element
        // if the List is not empty
        println(r)
        println(s)
    }
}
输出:
Some(7)
None

  • 这里, headOption返回有序集合的第一个元素,但如果集合为空,则返回None