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`.
|
* 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> {
|
public fun <T : Any> Array<out T?>.filterNotNull(): List<T> {
|
||||||
return filterNotNullTo(ArrayList<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`.
|
* 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> {
|
public fun <T : Any> Iterable<T?>.filterNotNull(): List<T> {
|
||||||
return filterNotNullTo(ArrayList<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`.
|
* Returns a sequence containing all elements that are not `null`.
|
||||||
*
|
*
|
||||||
* The operation is _intermediate_ and _stateless_.
|
* The operation is _intermediate_ and _stateless_.
|
||||||
|
*
|
||||||
|
* @sample samples.collections.Collections.Filtering.filterNotNull
|
||||||
*/
|
*/
|
||||||
public fun <T : Any> Sequence<T?>.filterNotNull(): Sequence<T> {
|
public fun <T : Any> Sequence<T?>.filterNotNull(): Sequence<T> {
|
||||||
@Suppress("UNCHECKED_CAST")
|
@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)
|
include(Iterables, Sequences, ArraysOfObjects)
|
||||||
} builder {
|
} builder {
|
||||||
doc { "Returns a list containing all elements that are not `null`." }
|
doc { "Returns a list containing all elements that are not `null`." }
|
||||||
|
sample("samples.collections.Collections.Filtering.filterNotNull")
|
||||||
typeParam("T : Any")
|
typeParam("T : Any")
|
||||||
returns("List<T>")
|
returns("List<T>")
|
||||||
toNullableT = true
|
toNullableT = true
|
||||||
|
|||||||
Reference in New Issue
Block a user