Add sample for filterNotNull
This commit is contained in:
@@ -3920,6 +3920,8 @@ public inline fun CharArray.filterNot(predicate: (Char) -> Boolean): List<Char>
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements that are not `null`.
|
||||
*
|
||||
* @sample samples.collections.Collections.Filtering.filterNotNull
|
||||
*/
|
||||
public fun <T : Any> Array<out T?>.filterNotNull(): List<T> {
|
||||
return filterNotNullTo(ArrayList<T>())
|
||||
|
||||
@@ -749,6 +749,8 @@ public inline fun <T> Iterable<T>.filterNot(predicate: (T) -> Boolean): List<T>
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements that are not `null`.
|
||||
*
|
||||
* @sample samples.collections.Collections.Filtering.filterNotNull
|
||||
*/
|
||||
public fun <T : Any> Iterable<T?>.filterNotNull(): List<T> {
|
||||
return filterNotNullTo(ArrayList<T>())
|
||||
|
||||
@@ -439,6 +439,8 @@ public fun <T> Sequence<T>.filterNot(predicate: (T) -> Boolean): Sequence<T> {
|
||||
* Returns a sequence containing all elements that are not `null`.
|
||||
*
|
||||
* The operation is _intermediate_ and _stateless_.
|
||||
*
|
||||
* @sample samples.collections.Collections.Filtering.filterNotNull
|
||||
*/
|
||||
public fun <T : Any> Sequence<T?>.filterNotNull(): Sequence<T> {
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
|
||||
@@ -801,4 +801,14 @@ class Collections {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Filtering {
|
||||
@Sample
|
||||
fun filterNotNull() {
|
||||
val numbers: List<Int?> = listOf(1, 2, null, 4)
|
||||
val nonNullNumbers = numbers.filterNotNull()
|
||||
|
||||
assertPrints(nonNullNumbers, "[1, 2, 4]")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -730,6 +730,7 @@ object Filtering : TemplateGroupBase() {
|
||||
include(Iterables, Sequences, ArraysOfObjects)
|
||||
} builder {
|
||||
doc { "Returns a list containing all elements that are not `null`." }
|
||||
sample("samples.collections.Collections.Filtering.filterNotNull")
|
||||
typeParam("T : Any")
|
||||
returns("List<T>")
|
||||
toNullableT = true
|
||||
|
||||
Reference in New Issue
Block a user