我遇到一种情况,需要像下面的代码所示,通过在内部应用一些过滤器来从集合中创建地图:
//Say I have a list
//I don't have to apply filter function ...
val myList = List(2,3,4,5)
val evenList = myList.map(x=>{
if ( x is even) x
else 0
}
//And the output is : List(2,0,4,0)
//The output actually needed was List(2,4) without applying filter on top like - ```myList.filter```
//I have objects instead of numbers of a case class so the output becomes :List(object1, None, object2, None)
But actual output needed was : List(object1,object2)
//更新的场景
val basket = List(2,4,5,6)
case class Apple(name:Option[String],size:Option[Int])
val listApples: List[Apple] = basket.map(x=>{
val r = new scala.util.Random
val size = r.nextInt(10)
if(x%2!=0){
Apple(None,None)
}
else Apple(Some("my-apple"),Some(size))
})
当前输出:
Apple(Some(my-apple),Some(2))
Apple(Some(my-apple),Some(0))
Apple(None,None)
Apple(Some(my-apple),Some(4))
Expected was :
Apple(Some(my-apple),Some(2))
Apple(Some(my-apple),Some(0))
Apple(Some(my-apple),Some(4))
答案 0 :(得分:5)
我相信collect
最适合您的情况。它使用部分函数作为参数,并且只有当该函数匹配时,元素才会被转换并添加到结果中:
val myList = List(2,3,4,5)
case class Wrapper(i: Int)
val evenList = myList.collect{
case x if x % 2 == 0 => Wrapper(x)
}
在这种情况下,只有2
和4
会被包裹在Wrapper
内:
List(Wrapper(2), Wrapper(4))
答案 1 :(得分:4)
我不确定我是否理解正确,但是为什么不直接使用filter
:
val myList = List(2,3,4,5)
myList.filter(_ % 2 == 0)
如果要将 Filter 作为功能:
def even(n:Int) = n % 2 == 0
myList.filter(even)
问题更新后,这里是filter
和collect
之间的区别:
过滤器:
myList
.filter(even)
.map(s => Apple(Some("my-apple"),Some(s)))
收集:
myList
.collect{ case s if(even(s)) => Apple(Some("my-apple"),Some(s))}
两个都返回List(Apple(Some(my-apple),Some(2)), Apple(Some(my-apple),Some(4)))
所以唯一的区别是您可以使用collect
一次完成两个步骤。
但是,对于我来说,将这两个步骤分开通常更具可读性。