Added functions for iterating and retrieving elements

This commit is contained in:
Anirban Chatterjee 2020-06-02 02:14:23 +02:00
parent 69a43cc613
commit ba924ce978
2 changed files with 94 additions and 4 deletions

View File

@ -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<String> {
return listOf("one", "two", "three")
val countryList = listOf("Germany", "India", "Japan", "Brazil")
return countryList
}
fun createMutableList(): MutableList<String> {
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
}

View File

@ -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())
}
}