Pregunta

Scala newbie here...

Doing below on address java object with getStLine*() methods... yields a List with empty strings for the empty strings returned. whats the Scala way to cleanly not add empty strings to a list.

val streets = List[String](addr.getStLine1, addr.getStLine2, addr.getStLine3)
¿Fue útil?

Solución

Looks like you're explicitely adding strings to list, I don't think you'd be able to easily avoid adding empty strings without custom derived list implementation. On the other hand you can easily filter out empty strings after the fact.

scala> val z = List("one", "", "three")
z: List[java.lang.String] = List(one, "", three)

scala> z.filter(p=> p!=null && !p.equals(""))
res2: List[java.lang.String] = List(one, three)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top