在Scala ListSet中,filter()方法用於選擇listSet中滿足指定謂詞的所有元素。
函數定義: def filter(p: (A) => Boolean): ListSet[A]
返回類型: It returns a new listset consisting all the elements of the listset which satisfies the given predicate.
範例1:
// Scala program of filter()
// method
import scala.collection.immutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating litset
val m1 = ListSet(1, 2, 3, 4, 5, 7)
// Applying filter method
val result = m1.filter(_>2)
// Displays output
println(result)
}
}
輸出:
ListSet(3, 4, 5, 7)
範例2:
// Scala program of filter()
// method
import scala.collection.immutable._
// Creating object
object GfG
{
// Main method
def main(args:Array[String])
{
// Creating litset
val m1 = ListSet(1, 2, 3, 4, 5, 7)
// Applying filter method
val result = m1.filter(_<1)
// Displays output
println(result)
}
}
輸出:
ListSet()
相關用法
- Scala ListSet min()用法及代碼示例
- Scala ListSet max()用法及代碼示例
- Scala ListSet sum()用法及代碼示例
- Scala ListSet filterNot()用法及代碼示例
- Scala ListSet find()用法及代碼示例
- Scala ListSet isEmpty()用法及代碼示例
- Scala ListSet count()用法及代碼示例
- Scala ListSet exists()用法及代碼示例
- Scala ListSet size()用法及代碼示例
- Scala ListSet product()用法及代碼示例
- Scala ListSet takeWhile()用法及代碼示例
- Scala Listset apply()用法及代碼示例
- Scala Map filter()用法及代碼示例
- Scala Set filter()用法及代碼示例
- Scala Stack filter()用法及代碼示例
注:本文由純淨天空篩選整理自IshwarGupta大神的英文原創作品 Scala ListSet filter() method with example。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。