當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Scala Iterator min()用法及代碼示例


min()方法屬於AbstractIterator類的具體值成員。它在IterableOnceOps類中定義。它用於查找最小的元素。

函數定義: def min[B >:A](implicit ord:math.Ordering[B]):A

Where, B is the type over which the ordering is defined and ord is an ordering to be used for comparing elements.



返回類型: It returns the smallest element of the collection stated with respect to the ordering ord.

範例1:

// Scala program of min() 
// method 
  
// Creating object 
object GfG 
{  
  
    // Main method 
    def main(args:Array[String]) 
    { 
      
        // Declaring an iterator 
        val iter = Iterator(50, 60, 11, 90, 21) 
          
        // Applying min method 
        val iter1 = iter.min 
          
        // Displays output 
        println(iter1) 
  
    } 
}
輸出:
11

在此,返回指定集合的​​最小元素。
範例2:

// Scala program of min() 
// method 
  
// Creating object 
object GfG 
{  
  
    // Main method 
    def main(args:Array[String]) 
    { 
      
        // Declaring an iterator 
        val iter = Iterator(2.1, 3.3, 1.1, 4.6) 
          
        // Applying min method 
        val iter1 = iter.min 
          
        // Displays output 
        println(iter1) 
  
    } 
}
輸出:
1.1


相關用法


注:本文由純淨天空篩選整理自nidhi1352singh大神的英文原創作品 Scala Iterator min() method with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。