Introducing windowed() and chunked() (#9490)
This commit is contained in:
parent
d89866aca7
commit
29ddb08808
@ -1,5 +1,6 @@
|
|||||||
package com.baeldung.collections
|
package com.baeldung.collections
|
||||||
|
|
||||||
|
import org.assertj.core.api.Assertions.assertThat
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
import kotlin.test.assertEquals
|
import kotlin.test.assertEquals
|
||||||
import kotlin.test.assertFalse
|
import kotlin.test.assertFalse
|
||||||
@ -8,7 +9,7 @@ import kotlin.test.assertTrue
|
|||||||
class CollectionsTest {
|
class CollectionsTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun whenUseDifferentCollections_thenSuccess () {
|
fun whenUseDifferentCollections_thenSuccess() {
|
||||||
val theList = listOf("one", "two", "three")
|
val theList = listOf("one", "two", "three")
|
||||||
assertTrue(theList.contains("two"))
|
assertTrue(theList.contains("two"))
|
||||||
|
|
||||||
@ -24,15 +25,15 @@ class CollectionsTest {
|
|||||||
assertTrue(theMutableSet.contains("four"))
|
assertTrue(theMutableSet.contains("four"))
|
||||||
|
|
||||||
val theMap = mapOf(1 to "one", 2 to "two", 3 to "three")
|
val theMap = mapOf(1 to "one", 2 to "two", 3 to "three")
|
||||||
assertEquals(theMap[2],"two")
|
assertEquals(theMap[2], "two")
|
||||||
|
|
||||||
val theMutableMap = mutableMapOf(1 to "one", 2 to "two", 3 to "three")
|
val theMutableMap = mutableMapOf(1 to "one", 2 to "two", 3 to "three")
|
||||||
theMutableMap[4] = "four"
|
theMutableMap[4] = "four"
|
||||||
assertEquals(theMutableMap[4],"four")
|
assertEquals(theMutableMap[4], "four")
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun whenSliceCollection_thenSuccess () {
|
fun whenSliceCollection_thenSuccess() {
|
||||||
val theList = listOf("one", "two", "three")
|
val theList = listOf("one", "two", "three")
|
||||||
val resultList = theList.slice(1..2)
|
val resultList = theList.slice(1..2)
|
||||||
|
|
||||||
@ -41,7 +42,7 @@ class CollectionsTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun whenJoinTwoCollections_thenSuccess () {
|
fun whenJoinTwoCollections_thenSuccess() {
|
||||||
val firstList = listOf("one", "two", "three")
|
val firstList = listOf("one", "two", "three")
|
||||||
val secondList = listOf("four", "five", "six")
|
val secondList = listOf("four", "five", "six")
|
||||||
val resultList = firstList + secondList
|
val resultList = firstList + secondList
|
||||||
@ -52,7 +53,7 @@ class CollectionsTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun whenFilterNullValues_thenSuccess () {
|
fun whenFilterNullValues_thenSuccess() {
|
||||||
val theList = listOf("one", null, "two", null, "three")
|
val theList = listOf("one", null, "two", null, "three")
|
||||||
val resultList = theList.filterNotNull()
|
val resultList = theList.filterNotNull()
|
||||||
|
|
||||||
@ -60,9 +61,9 @@ class CollectionsTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun whenFilterNonPositiveValues_thenSuccess () {
|
fun whenFilterNonPositiveValues_thenSuccess() {
|
||||||
val theList = listOf(1, 2, -3, -4, 5, -6)
|
val theList = listOf(1, 2, -3, -4, 5, -6)
|
||||||
val resultList = theList.filter{ it > 0}
|
val resultList = theList.filter { it > 0 }
|
||||||
//val resultList = theList.filter{ x -> x > 0}
|
//val resultList = theList.filter{ x -> x > 0}
|
||||||
|
|
||||||
assertEquals(3, resultList.size)
|
assertEquals(3, resultList.size)
|
||||||
@ -71,7 +72,7 @@ class CollectionsTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun whenDropFirstItems_thenRemoved () {
|
fun whenDropFirstItems_thenRemoved() {
|
||||||
val theList = listOf("one", "two", "three", "four")
|
val theList = listOf("one", "two", "three", "four")
|
||||||
val resultList = theList.drop(2)
|
val resultList = theList.drop(2)
|
||||||
|
|
||||||
@ -81,9 +82,9 @@ class CollectionsTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun whenDropFirstItemsBasedOnCondition_thenRemoved () {
|
fun whenDropFirstItemsBasedOnCondition_thenRemoved() {
|
||||||
val theList = listOf("one", "two", "three", "four")
|
val theList = listOf("one", "two", "three", "four")
|
||||||
val resultList = theList.dropWhile{ it.length < 4 }
|
val resultList = theList.dropWhile { it.length < 4 }
|
||||||
|
|
||||||
assertEquals(2, resultList.size)
|
assertEquals(2, resultList.size)
|
||||||
assertFalse(resultList.contains("one"))
|
assertFalse(resultList.contains("one"))
|
||||||
@ -91,7 +92,7 @@ class CollectionsTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun whenExcludeItems_thenRemoved () {
|
fun whenExcludeItems_thenRemoved() {
|
||||||
val firstList = listOf("one", "two", "three")
|
val firstList = listOf("one", "two", "three")
|
||||||
val secondList = listOf("one", "three")
|
val secondList = listOf("one", "three")
|
||||||
val resultList = firstList - secondList
|
val resultList = firstList - secondList
|
||||||
@ -101,16 +102,16 @@ class CollectionsTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun whenSearchForExistingItem_thenFound () {
|
fun whenSearchForExistingItem_thenFound() {
|
||||||
val theList = listOf("one", "two", "three")
|
val theList = listOf("one", "two", "three")
|
||||||
|
|
||||||
assertTrue("two" in theList)
|
assertTrue("two" in theList)
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun whenGroupItems_thenSuccess () {
|
fun whenGroupItems_thenSuccess() {
|
||||||
val theList = listOf(1, 2, 3, 4, 5, 6)
|
val theList = listOf(1, 2, 3, 4, 5, 6)
|
||||||
val resultMap = theList.groupBy{ it % 3}
|
val resultMap = theList.groupBy { it % 3 }
|
||||||
|
|
||||||
assertEquals(3, resultMap.size)
|
assertEquals(3, resultMap.size)
|
||||||
print(resultMap[1])
|
print(resultMap[1])
|
||||||
@ -119,28 +120,93 @@ class CollectionsTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun whenApplyFunctionToAllItems_thenSuccess () {
|
fun whenApplyFunctionToAllItems_thenSuccess() {
|
||||||
val theList = listOf(1, 2, 3, 4, 5, 6)
|
val theList = listOf(1, 2, 3, 4, 5, 6)
|
||||||
val resultList = theList.map{ it * it }
|
val resultList = theList.map { it * it }
|
||||||
print(resultList)
|
print(resultList)
|
||||||
assertEquals(4, resultList[1])
|
assertEquals(4, resultList[1])
|
||||||
assertEquals(9, resultList[2])
|
assertEquals(9, resultList[2])
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun whenApplyMultiOutputFunctionToAllItems_thenSuccess () {
|
fun whenApplyMultiOutputFunctionToAllItems_thenSuccess() {
|
||||||
val theList = listOf("John", "Tom")
|
val theList = listOf("John", "Tom")
|
||||||
val resultList = theList.flatMap{ it.toLowerCase().toList()}
|
val resultList = theList.flatMap { it.toLowerCase().toList() }
|
||||||
print(resultList)
|
print(resultList)
|
||||||
assertEquals(7, resultList.size)
|
assertEquals(7, resultList.size)
|
||||||
assertTrue(resultList.contains('j'))
|
assertTrue(resultList.contains('j'))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun whenApplyFunctionToAllItemsWithStartingValue_thenSuccess () {
|
fun whenApplyFunctionToAllItemsWithStartingValue_thenSuccess() {
|
||||||
val theList = listOf(1, 2, 3, 4, 5, 6)
|
val theList = listOf(1, 2, 3, 4, 5, 6)
|
||||||
val finalResult = theList.fold( 1000, { oldResult, currentItem -> oldResult + (currentItem *currentItem)})
|
val finalResult = theList.fold(1000, { oldResult, currentItem -> oldResult + (currentItem * currentItem) })
|
||||||
print(finalResult)
|
print(finalResult)
|
||||||
assertEquals(1091, finalResult)
|
assertEquals(1091, finalResult)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun whenApplyingChunked_thenShouldBreakTheCollection() {
|
||||||
|
val theList = listOf(1, 2, 3, 4, 5)
|
||||||
|
val chunked = theList.chunked(2)
|
||||||
|
|
||||||
|
assertThat(chunked.size).isEqualTo(3)
|
||||||
|
assertThat(chunked.first()).contains(1, 2)
|
||||||
|
assertThat(chunked[1]).contains(3, 4)
|
||||||
|
assertThat(chunked.last()).contains(5)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun whenApplyingChunkedWithTransformation_thenShouldBreakTheCollection() {
|
||||||
|
val theList = listOf(1, 2, 3, 4, 5)
|
||||||
|
val chunked = theList.chunked(3) { it.joinToString(", ") }
|
||||||
|
|
||||||
|
assertThat(chunked.size).isEqualTo(2)
|
||||||
|
assertThat(chunked.first()).isEqualTo("1, 2, 3")
|
||||||
|
assertThat(chunked.last()).isEqualTo("4, 5")
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun whenApplyingWindowed_thenShouldCreateSlidingWindowsOfElements() {
|
||||||
|
val theList = (1..6).toList()
|
||||||
|
val windowed = theList.windowed(3)
|
||||||
|
|
||||||
|
assertThat(windowed.size).isEqualTo(4)
|
||||||
|
assertThat(windowed.first()).contains(1, 2, 3)
|
||||||
|
assertThat(windowed[1]).contains(2, 3, 4)
|
||||||
|
assertThat(windowed[2]).contains(3, 4, 5)
|
||||||
|
assertThat(windowed.last()).contains(4, 5, 6)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun whenApplyingWindowedWithTwoSteps_thenShouldCreateSlidingWindowsOfElements() {
|
||||||
|
val theList = (1..6).toList()
|
||||||
|
val windowed = theList.windowed(size = 3, step = 2)
|
||||||
|
|
||||||
|
assertThat(windowed.size).isEqualTo(2)
|
||||||
|
assertThat(windowed.first()).contains(1, 2, 3)
|
||||||
|
assertThat(windowed.last()).contains(3, 4, 5)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun whenApplyingPartialWindowedWithTwoSteps_thenShouldCreateSlidingWindowsOfElements() {
|
||||||
|
val theList = (1..6).toList()
|
||||||
|
val windowed = theList.windowed(size = 3, step = 2, partialWindows = true)
|
||||||
|
|
||||||
|
assertThat(windowed.size).isEqualTo(3)
|
||||||
|
assertThat(windowed.first()).contains(1, 2, 3)
|
||||||
|
assertThat(windowed[1]).contains(3, 4, 5)
|
||||||
|
assertThat(windowed.last()).contains(5, 6)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
fun whenApplyingTransformingWindows_thenShouldCreateSlidingWindowsOfElements() {
|
||||||
|
val theList = (1..6).toList()
|
||||||
|
val windowed = theList.windowed(size = 3, step = 2, partialWindows = true) { it.joinToString(", ") }
|
||||||
|
|
||||||
|
assertThat(windowed.size).isEqualTo(3)
|
||||||
|
assertThat(windowed.first()).isEqualTo("1, 2, 3")
|
||||||
|
assertThat(windowed[1]).isEqualTo("3, 4, 5")
|
||||||
|
assertThat(windowed.last()).isEqualTo("5, 6")
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user