Created
May 31, 2018 21:20
-
-
Save javadude/9f4456211f2ab9fdfced7405530bd2eb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package streams | |
import kotlin.reflect.KClass | |
import kotlin.reflect.full.companionObject | |
import kotlin.system.measureNanoTime | |
val list1 = listOf(1,2,3,4,5,6,7,8,9,10) | |
val iteratorList = CustomList("iterator", list1) | |
val sequenceList = CustomList("sequence", list1) | |
class CustomList(val name : String, val baseList : List<Int>) | |
: List<Int> by baseList { | |
override fun iterator() : Iterator<Int> { | |
val baseIterator = baseList.iterator() | |
return object : Iterator<Int> { | |
override fun hasNext() = baseIterator.hasNext() | |
override fun next(): Int { | |
val value = baseIterator.next() | |
println("$name.next() called : $value") | |
return value | |
} | |
} | |
} | |
} | |
class Person(val name : String) : Comparable<Person> { | |
override fun compareTo(other: Person) = name.compareTo(other.name) | |
} | |
val personList = listOf(Person("Scott"), Person("Steve"), Person("Mike"), Person("Chewie")) | |
val words = "Once upon a time" | |
val family = mapOf( | |
"siblings" to listOf("Steve"), | |
"parents" to listOf("Oliver", "Margaret"), | |
"children" to listOf("Alex", "Trevor", "Nicole", "Claire") | |
) | |
val family2 = mapOf( | |
"siblings" to listOf(listOf("Steve")), | |
"parents" to listOf(listOf("Oliver"), listOf("Margaret")), | |
"children" to listOf(listOf("Alex"), listOf("Trevor"), listOf("Nicole"), listOf("Claire")) | |
) | |
val familyPeople = mapOf( | |
"siblings" to listOf(Person("Steve")), | |
"parents" to listOf(Person("Oliver"), Person("Margaret")), | |
"children" to listOf(Person("Alex"), Person("Trevor"), Person("Nicole"), Person("Claire")) | |
) | |
fun main(args: Array<String>) { | |
var newList = list1.filter { it % 2 == 0} | |
newList.forEach { println(it) } | |
println("----------------") | |
newList = list1.filter { it % 2 == 0}.take(2) | |
newList.forEach { println(it) } | |
println("----------------") | |
println("----------------") | |
newList = iteratorList.filter { it % 2 == 0} | |
newList.forEach { println(it) } | |
println("----------------") | |
newList = iteratorList.filter { it % 2 == 0}.take(2) | |
newList.forEach { println(it) } | |
println("----------------") | |
println("----------------") | |
newList = sequenceList.asSequence().filter { it % 2 == 0}.toList() | |
newList.forEach { println(it) } | |
println("----------------") | |
newList = sequenceList.asSequence().filter { it % 2 == 0}.take(2).toList() | |
newList.forEach { println(it) } | |
println(sequenceList.asSequence().filter { it % 2 == 0}.first()) | |
val l1 = listOf("a", "b") | |
val l2 = mutableListOf("a", "b") | |
l2.add("c") | |
val m1 = mapOf( | |
"a" to "b", | |
"c" to "d" | |
) | |
val m2 = mutableMapOf( | |
"a" to "b", | |
"c" to "d" | |
) | |
m2["e"] = "f" | |
m1["a"] | |
l1.filterNotNull() | |
list1.min() | |
list1.max() | |
personList.min() | |
list1.average() | |
list1.reversed() | |
list1.asReversed() | |
personList.firstOrNull() | |
val r = personList.map { it.name } | |
personList.mapIndexed {n , item -> n - 1} | |
personList.drop(2).forEach { println(it.name) } | |
println("---") | |
personList.take(2).forEach { println(it.name) } | |
println("---") | |
personList.sorted().forEach { println(it.name) } | |
println("---") | |
personList.sortedWith(Comparator.comparing(Person::name)).forEach { println(it.name) } | |
personList.sortedBy(Person::name).forEach { println(it.name) } | |
personList.filterIsInstance(Person::class.java) | |
words.splitToSequence(" ").map { it.length }.reduce { total, wordLength -> wordLength + total} | |
println("---") | |
println(family.map { it.value }) | |
println(family.flatMap { it.value }) | |
println(family2.flatMap { it.value }) | |
println(family2.flatMap { it.value }.flatMap { it }.joinToString("|","<<<", ">>>")) | |
println(measureNanoTime { | |
println(familyPeople | |
.flatMap { it.value } | |
.sortedBy(Person::name) | |
.joinToString()) | |
}) | |
println(measureNanoTime { | |
println(familyPeople | |
.flatMap { it.value } | |
.asSequence() | |
.sortedBy(Person::name) | |
.joinToString()) | |
}) | |
println(measureNanoTime { | |
println(familyPeople | |
.flatMap { it.value } | |
.sortedBy(Person::name) | |
.asSequence() | |
.joinToString()) | |
}) | |
} | |
interface EnumStuff<T> { | |
fun values() : Array<T> | |
} | |
class A1 { | |
companion object : EnumStuff<A1> { | |
override fun values(): Array<A1> { | |
return arrayOf(A1()) | |
} | |
} | |
} | |
class A2 { | |
companion object : EnumStuff<A2> { | |
override fun values(): Array<A2> { | |
return arrayOf(A2()) | |
} | |
} | |
} | |
class Foo { | |
companion object { | |
@JvmStatic | |
fun main(args: Array<String>) { | |
} | |
fun <T:Any> printValues(type : KClass<T>) { | |
(type.companionObject as EnumStuff<T>).values() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment