From ba924ce978c9ea3ed82677ebd8fd497f3c4f90fe Mon Sep 17 00:00:00 2001 From: Anirban Chatterjee Date: Tue, 2 Jun 2020 02:14:23 +0200 Subject: [PATCH] Added functions for iterating and retrieving elements --- .../com/baeldung/collections/ListExample.kt | 79 ++++++++++++++++++- .../collections/ListExampleUnitTest.kt | 19 ++++- 2 files changed, 94 insertions(+), 4 deletions(-) diff --git a/core-kotlin-modules/core-kotlin-collections/src/main/kotlin/com/baeldung/collections/ListExample.kt b/core-kotlin-modules/core-kotlin-collections/src/main/kotlin/com/baeldung/collections/ListExample.kt index a0dd04a760..8b8028f613 100644 --- a/core-kotlin-modules/core-kotlin-collections/src/main/kotlin/com/baeldung/collections/ListExample.kt +++ b/core-kotlin-modules/core-kotlin-collections/src/main/kotlin/com/baeldung/collections/ListExample.kt @@ -3,11 +3,86 @@ package com.baeldung.collections import kotlin.collections.List class ListExample { + + private val countries = listOf("Germany", "India", "Japan", "Brazil", "Australia") + private val cities = mutableListOf("Berlin", "Calcutta", "Seoul", "Sao Paulo", "Sydney") + fun createList(): List { - return listOf("one", "two", "three") + val countryList = listOf("Germany", "India", "Japan", "Brazil") + return countryList } fun createMutableList(): MutableList { - return mutableListOf("Berlin", "Kolkata", "London") + val cityList = mutableListOf("Berlin", "Calcutta", "Seoul", "Sao Paulo") + return cityList } + + fun iterateUsingForLoop() { + countries.forEach { it -> print("$it ") } + println() + + for (country in countries) { + print("$country ") + } + println() + + for (i in 0 until countries.size) { + print("${countries[i]} ") + } + println() + + countries.forEachIndexed { i, e -> + println("country[$i] = $e") + } + } + + fun iterateUsingListIterator() { + val iterator = countries.listIterator() + while (iterator.hasNext()) { + val country = iterator.next() + print("$country ") + } + println() + + while (iterator.hasPrevious()) { + println("Index: ${iterator.previousIndex()}") + } + } + + fun iterateUsingIterator() { + val iterator = cities.iterator() + iterator.next() + iterator.remove() + println(cities) + } + + fun iterateUsingMutableListIterator() { + val iterator = cities.listIterator(1) + iterator.next() + iterator.add("London") + iterator.next() + iterator.set("Milan") + println(cities) + } + + fun retrieveElementsInList(): String { + println(countries[2]) + return countries[2] + } + + fun retrieveElementsUsingGet(): String { + println(countries.get(3)) + return countries.get(3) + } + + fun retrieveElementsFirstAndLast(): String? { + println(countries.first()) + println(countries.last()) + println(countries.first { it.length > 7 }) + println(countries.last { it.startsWith("J") }) + println(countries.firstOrNull { it.length > 8 }) + return countries.firstOrNull { it.length > 8 } + } + + //5. Retrieve parts of the list } \ No newline at end of file diff --git a/core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/collections/ListExampleUnitTest.kt b/core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/collections/ListExampleUnitTest.kt index 46e4d09369..f4a7be0ec0 100644 --- a/core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/collections/ListExampleUnitTest.kt +++ b/core-kotlin-modules/core-kotlin-collections/src/test/kotlin/com/baeldung/collections/ListExampleUnitTest.kt @@ -3,6 +3,7 @@ package com.baeldung.collections import org.junit.jupiter.api.Test import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.api.Assertions.assertNotEquals +import kotlin.test.assertNull import kotlin.test.assertTrue class ListExampleUnitTest { @@ -11,8 +12,22 @@ class ListExampleUnitTest { @Test fun whenListIsCreated_thenContainsElements() { - assertTrue(classUnderTest.createList().contains("two")) - assertTrue(classUnderTest.createMutableList().contains("Berlin")) + assertTrue(classUnderTest.createList().contains("India")) + assertTrue(classUnderTest.createMutableList().contains("Seoul")) } + @Test + fun whenRetrieveElementsInList_thenSuccess() { + assertEquals("Japan", classUnderTest.retrieveElementsInList()) + } + + @Test + fun whenRetrieveElementsUsingGet_thenSuccess() { + assertEquals("Brazil", classUnderTest.retrieveElementsUsingGet()) + } + + @Test + fun whenRetrieveElementsFirstAndLast_thenSuccess() { + assertEquals("Australia", classUnderTest.retrieveElementsFirstAndLast()) + } } \ No newline at end of file