Scala Trait Traversable 第3部分

Scala Trait Traversable 第3部分

操作如下。
* 子集合的检索操作。
这里的操作有slice, drop, dropWhile, filter, filterNot, tail, take, takeWhile, and init . 这些操作被用来返回一些子集合。

示例 :

// Scala program of Sub-Collection
// retrieval operation
  
// Creating object 
object SubCollection
{
  
    // Main method
    def main(args: Array[String]) 
    {
  
        // Creating List of numbers 
        val x = List(17, 19, 21, 29, 31)
  
        // Applying Sub-Collection 
        // retrieval operation
        val y = x.init 
  
        // Displays all the elements of 
        // the List except the last one
        println(y)
    }
}

输出:

List(17, 19, 21, 29)

这里,init检索了Traversable集合的所有元素,除了最后一个元素。

示例 :

// Scala program of Sub-Collection
// retrieval operation
  
// Creating object 
object SubCollection
{
  
    // Main method
    def main(args: Array[String]) 
    {
  
        // Creating List of numbers 
        val x = List(17, 19, 21, 29, 31)
  
        // Applying Sub-Collection 
        // retrieval operation
        val y = x.tail
  
        // Displays all the elements of 
        // the List except the first one
        println(y)
    }
}

输出:

List(19, 21, 29, 31)

在这里,tail检索了Traversable集合中除第一个元素以外的所有元素。

示例 :

// Scala program of Sub-Collection
// retrieval operation
  
// Creating object 
object SubCollection
{
  
    // Main method
    def main(args: Array[String]) 
    {
  
        // Creating List of numbers 
        val x = List(27, 29, 31, 49, 51, 56)
  
        // Applying Sub-Collection 
        // retrieval operation
        val y = x.slice(2, 5)
  
        // Displays the elements 
        // from index 2 to 4 
        println(y)
    }
}

输出:

List(31, 49, 51)

这里,slice将返回从给定的索引范围内的元素,不包括最后的索引。

示例 :

// Scala program of Sub-Collection
// retrieval operation
  
// Creating object 
object SubCollection
{
  
    // Main method
    def main(args: Array[String]) 
    {
  
        // Creating List of numbers 
        val x = List(37, 49, 51, 69, 71, 86)
  
        // Applying Sub-Collection 
        // retrieval operation
        val y = x.take(2)
  
        // Displays the first two 
        // elements of the list
        println(y)
    }
}

输出:

List(37, 49)

在这里,这个操作将返回在take操作的参数中给出的元素的数量。

示例 :

// Scala program of Sub-Collection
// retrieval operation
  
// Creating object 
object SubCollection
{
  
    // Main method
    def main(args: Array[String]) 
    {
  
        // Creating List of numbers 
        val x = List(33, 34, 36, 37, 39, 40, 44)
  
        // Applying Sub-Collection 
        // retrieval operation
        val y = x.drop(4)
  
        // Displays all the elements of 
        // the list except the first 
        // four elements
        println(y)
    }
}

输出:

List(39, 40, 44)

在这里,这个操作将返回集合中的所有元素,除了在drop操作的参数中给出的第一个元素的数量。

示例:

// Scala program of Sub-Collection
// retrieval operation
  
// Creating object 
object SubCollection
{
  
    // Main method
    def main(args: Array[String]) 
    {
  
        // Creating List of numbers 
        val x = List(2, 5, 9, 13, 17, 20)
  
        // Applying Sub-Collection 
        // retrieval operation
        val y = x.dropWhile(_ < 10)
  
        // Displays all the elements of 
        // the list which are greater 
        // than or equal to 10
        println(y)
    }
}

输出:

List(13, 17, 20)

在这里,dropWhile将删除元素,直到给定的条件得到满足,并返回剩下的元素。

示例 :

// Scala program of Sub-Collection
// retrieval operation
  
// Creating object 
object SubCollection
{
  
    // Main method
    def main(args: Array[String]) 
    {
  
        // Creating List of numbers 
        val x = List(2, 5, 9, 10, 13, 17, 20)
  
        // Applying Sub-Collection 
        // retrieval operation
        val y = x.takeWhile(_ < 13)
          
        // Displays all the elements of 
        // the list which are less 
        // than 13
        println(y)
    }
}

输出:

List(2, 5, 9, 10)

在这里,takeWhile将采取元素,直到给定的条件得到满足,并返回这些元素。

示例 :

// Scala program of Sub-Collection
// retrieval operation
  
// Creating object 
object SubCollection
{
  
    // Main method
    def main(args: Array[String]) 
    {
  
        // Creating List of numbers 
        val x = List(9, 65, 99, 10, 23, 17, 12)
  
        // Applying Sub-Collection 
        // retrieval operation
        val y = x.filter(_ < 23)
  
        // Displays all the elements of 
        // the list which are less 
        // than 23
        println(y)
    }
}

输出:

List(9, 10, 17, 12)

在这里,过滤器将返回所有的元素,直到给定的条件得到满足,并丢弃其余的元素。

示例 :

// Scala program of Sub-Collection
// retrieval operation
  
// Creating object 
object SubCollection
{
  
    // Main method
    def main(args: Array[String]) 
    {
  
        // Creating List of numbers 
        val x = List(9, 65, 99, 10, 23, 17, 12)
  
        // Applying Sub-Collection 
        // retrieval operation
        val y = x.filterNot(_ < 23)
  
        // Displays all the elements of 
        // the list which are greater 
        // than or equal to 23
        println(y)
    }
}

Output:

List(65, 99, 23)

在这里,filterNot将返回所有不符合给定条件的元素,并丢弃其余的元素。

  • 细分操作。
    这些操作包括span, partition, splitAt, groupBy . 利用这些操作可以打破集合中的元素并返回一些子集合。

示例 :

// Scala program of Subdivision 
// operations
  
// Creating object 
object Subdivision
{
  
    // Main method
    def main(args: Array[String]) 
    {
  
        // Creating List of numbers 
        val q = List(7, 9, 11, 15, 17, 19, 22)
  
        // Applying Subdivision 
        // operations
        val r = q.span(_ < 15)
  
        // Displays all the elements in 
        // two parts
        println(r)
    }
}

Output:

(List(7, 9, 11), List(15, 17, 19, 22))

在这里,span将把给定的Traversable元素集合分成两部分,其中,第一部分是通过执行takeWhile操作得到的,第二部分是通过执行dropWhile操作得到的。

示例 :

// Scala program of Subdivision 
// operations
  
// Creating object 
object Subdivision
{
  
    // Main method
    def main(args: Array[String]) 
    {
  
        // Creating List of numbers 
        val q = List(17, 29, 31, 36, 37, 39, 42)
  
        // Applying Subdivision 
        // operations
        val r = q.partition(_ < 35)
  
        // Displays all the elements in 
        // two parts
        println(r)
    }
}

输出:

(List(17, 29, 31), List(36, 37, 39, 42))

在这里,分区将把集合分成两部分,第一部分将返回满足给定条件的元素,第二部分将返回其余元素。

示例 :

// Scala program of Subdivision 
// operations
  
// Creating object 
object Subdivision
{
  
    // Main method
    def main(args: Array[String]) 
    {
  
        // Creating List of numbers 
        val q = List(17, 29, 31, 36, 37, 39, 42)
  
        // Applying Subdivision 
        // operations
        val r = q.splitAt(4)
  
        // Displays all the elements in 
        // two parts
        println(r)
    }
}

输出:

(List(17, 29, 31, 36), List(37, 39, 42))

在这里,splitAt将把集合中的元素在给定的位置分成两部分,其中,第一部分将通过执行take操作获得,第二部分将通过执行drop操作获得。

示例 :

// Scala program of Subdivision 
// operations
  
// Creating object 
object Subdivision
{
    // Main method
    def main(args: Array[String]) 
    {
  
        // Creating List of numbers 
        val x = List(21, 20, 33, 40, 27, 62)
  
        // Applying Subdivision 
        // operation 
        val y = x.groupBy 
        {
  
            // Partial function
            case z : Int if (z % 3 == 0) => z + 3
            case z : Int if (z % 2 == 0) => z + 2
        }
      
        // Displaying Map of lists
        println(y)
    }
}

输出:

Map(42 -> List(40), 24 -> List(21), 64 -> List(62), 22 -> List(20), 36 -> List(33), 30 -> List(27))

这里,groupBy将根据给定的部分函数划分Traversable集合,并返回一个Map。

  • 元素测试方法。
    这些方法包括forall, exists, 和 count 。这些方法被用来在既定条件的基础上测试给定的Traversable集合。

示例 :

// Scala program of Element 
// test
  
// Creating object 
object Elementtest
{
  
    // Main method
    def main(args: Array[String]) 
    {
  
        // Creating List of numbers 
        val x = List(21, 20, 33, 40, 27, 62)
  
        // Applying Element test 
        // method
        val y = x forall (_ < 63) 
  
        // Displays true if all the 
        // elements are lesser 
        // than 63
        println(y)
    }
}

输出:

true

在这里,如果集合中的所有元素都满足给定的条件,forall将返回真,否则返回假。

示例 :

// Scala program of Element 
// test
  
// Creating object 
object Elementtest
{
  
    // Main method
    def main(args: Array[String]) 
    {
  
        // Creating List of numbers 
        val x = List(21, 20, 33, 40, 27, 62)
  
        // Applying Element test 
        // method
        val y = x exists (_ < 30) 
  
        // Displays true if some of 
        // the elements are lesser 
        // than 30
        println(y)
    }
}

输出:

true

在这里,如果集合中的某些元素满足给定的条件,existence将返回true,否则返回false。

示例 :

// Scala program of Element 
// test
  
// Creating object 
object Elementtest
{
  
    // Main method
    def main(args: Array[String]) 
    {
  
        // Creating List of numbers 
        val x = List(21, 20, 33, 40, 27, 62)
  
        // Applying Element test 
        // method
        val y = x count(_ < 40) 
  
        // Displays the number of 
        // elements satisfying the 
        // given condition
        println(y)
    }
}

输出:

4

这里,count将显示集合中满足给定条件的元素的数量。

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程