All Downloads are FREE. Search and download functionalities are using the official Maven repository.

name.remal.java.util.stream.Stream.kt Maven / Gradle / Ivy

The newest version!
package name.remal

import edu.umd.cs.findbugs.annotations.SuppressFBWarnings
import java.util.Comparator
import java.util.HashMap
import java.util.HashSet
import java.util.LinkedHashMap
import java.util.SortedMap
import java.util.SortedSet
import java.util.TreeMap
import java.util.TreeSet
import java.util.stream.Collectors
import java.util.stream.Stream
import java.util.stream.StreamSupport

fun  emptyStream(): Stream = Stream.of()
fun  streamOf(): Stream = Stream.of()
fun  streamOf(vararg elements: T): Stream = Stream.of(*elements)

operator fun  Stream.plus(other: Stream): Stream = Stream.concat(this, other)

fun  Stream.filterNotNull(): Stream = filter { it != null }.uncheckedCast()
fun  Stream.filterIsInstance(type: Class): Stream = filter { type.isInstance(it) }.uncheckedCast()
@SuppressFBWarnings("UPM_UNCALLED_PRIVATE_METHOD")
inline fun  Stream.filterIsInstance(): Stream = filterIsInstance(R::class.java)

@JvmName("flatMapCollection")
fun  Stream.flatMap(mapper: (T) -> Collection): Stream = this.flatMap { mapper(it).stream() }

@JvmName("flatMapIterable")
fun  Stream.flatMap(mapper: (T) -> Iterable): Stream = this.flatMap { StreamSupport.stream(mapper(it).spliterator(), false) }


fun  Stream.firstOrNull(): T? = findFirst().orNull

fun  Stream.asIterable(): Iterable = object : Iterable {
    override fun iterator() = [email protected]()
}

fun  Stream.toList(): List = collect(Collectors.toList())
fun  Stream.toSet(): Set = collect(Collectors.toCollection(::LinkedHashSet))
fun  Stream.toHashSet(): Set = collect(Collectors.toCollection(::HashSet))
fun > Stream.toSortedSet(): SortedSet = collect(Collectors.toCollection { TreeSet() })
fun > Stream.toMap(): Map = collect(Collectors.toMap({ it.first }, { it.second }, { _, value -> value }, ::LinkedHashMap))
fun > Stream.toHashMap(): Map = collect(Collectors.toMap({ it.first }, { it.second }, { _, value -> value }, ::HashMap))
fun , V, T : Pair> Stream.toSortedMap(): SortedMap = collect(Collectors.toMap({ it.first }, { it.second }, { _, value -> value }, ::TreeMap))
fun  Stream.toMap(keyExtractor: (value: V) -> K): Map = collect(Collectors.toMap(keyExtractor, { it }, { _, value -> value }, ::LinkedHashMap))
fun  Stream.toHashMap(keyExtractor: (value: V) -> K): Map = collect(Collectors.toMap(keyExtractor, { it }, { _, value -> value }, ::HashMap))
fun > Stream.toSortedMap(keyExtractor: (value: V) -> K): Map = collect(Collectors.toMap(keyExtractor, { it }, { _, value -> value }, ::TreeMap))

@JvmName("joinCharSequencesToString")
fun  Stream.joinToString(separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = ""): String = collect(Collectors.joining(separator, prefix, postfix))

fun  Stream.joinToString(separator: CharSequence = ", ", prefix: CharSequence = "", postfix: CharSequence = ""): String = map { "$it" }.joinToString(separator, prefix, postfix)

fun > Stream.max(): T? = max(Comparator.naturalOrder()).orNull
fun > Stream.min(): T? = min(Comparator.naturalOrder()).orNull

fun  Stream.reduce(reducer: (first: T, second: T) -> T): T? = collect(Collectors.reducing(reducer)).orNull




© 2015 - 2025 Weber Informatics LLC | Privacy Policy