Как удалить нули в моей переменной списка? - PullRequest
0 голосов
/ 30 июня 2018

У меня есть следующий список ввода:

List(List(first, sec, null, null), List(third, null, null))

Мне нужно удалить null s из моего списка, чтобы получить:

List(List(first, sec), List(third))

Ответы [ 2 ]

0 голосов
/ 30 июня 2018

Учитывая этот список:

val list = List(List("first", "sec", null, null), List("third", null, null))

А то, что Option(null) дает None, то:

list.map(_.flatMap(Option(_)))

производит:

List(List("first", "sec"), List("third"))
0 голосов
/ 30 июня 2018

Если у вас есть вложенный список как

val data = List(List("first", "sec", null, null), List("third", null, null))

Затем используйте map и filterNot как

data.map(_.filterNot(_ == null))
//res0: List[List[String]] = List(List(first, sec), List(third))

Или вы можете использовать map и filter как

data.map(_.filter(_ != null))
//res0: List[List[String]] = List(List(first, sec), List(third))

Надеюсь, ответ полезен

...