Java – search the scala list to find what matches the attribute

What are the usual Scala methods? I have a list. If I find something that meets certain conditions, I want to return "Y", otherwise I want to return "n" I have an "effective" solution, but I don't like it

def someMethod( someList: List[Something]) : String = {

someList.foreach( a =>
  if (a.blah.equals("W") || a.bar.equals("Y") ) {
    return "Y"
  }
 )
  "N"


}

Solution

Simples:

if (someList.exists{ a=> a.blah == "W" || a.bar == "Y"}) 
   "Y"
else 
   "N"
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>