From db695999982446070b4cd0b10c9159358be2920b Mon Sep 17 00:00:00 2001 From: Anirban Chatterjee Date: Thu, 4 Jun 2020 18:07:25 +0200 Subject: [PATCH] Added functions for list write and sort operations --- .../com/baeldung/collections/ListExample.kt | 96 ++++++++++++++++++- .../collections/ListExampleUnitTest.kt | 75 +++++++++++++++ 2 files changed, 170 insertions(+), 1 deletion(-) 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 8b8028f613..38391a1049 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 @@ -84,5 +84,99 @@ class ListExample { return countries.firstOrNull { it.length > 8 } } - //5. Retrieve parts of the list + fun retrieveSubList(): List { + val subList = countries.subList(1, 4) + println(subList) + return subList + } + + fun retrieveListSliceUsingIndices(): List { + val sliceList = countries.slice(1..4) + println(sliceList) + return sliceList + } + + fun retrieveListSliceUsingIndicesList(): List { + val sliceList = countries.slice(listOf(1, 4)) + println(sliceList) + return sliceList + } + + fun countList(): Int { + val count = countries.count() + println(count) + return count + } + + fun countListUsingPredicate(): Int { + val count = countries.count { it.length > 5 } + println(count) + return count + } + + fun countListUsingProperty(): Int { + val size = countries.size + println(size) + return size + } + + fun addToList(): List { + cities.add("Barcelona") + println(cities) + cities.add(3, "London") + println(cities) + cities.addAll(listOf("Singapore", "Moscow")) + println(cities) + cities.addAll(2, listOf("Prague", "Amsterdam")) + println(cities) + return cities + } + + fun removeFromList(): List { + cities.remove("Seoul") + println(cities) + cities.removeAt(1) + println(cities) + return cities + } + + fun replaceFromList(): List { + cities.set(3, "Prague") + println(cities) + cities[4] = "Moscow" + println(cities) + cities.fill("Barcelona") + println(cities) + return cities + } + + fun sortMutableList(): List { + cities.sort() + println(cities) + cities.sortDescending() + println(cities) + return cities + } + + fun sortList(): List { + val sortedCountries = countries.sorted() + println("countries = $countries") + println("sortedCountries = $sortedCountries") + val sortedCountriesDescending = countries.sortedDescending() + println("countries = $countries") + println("sortedCountriesDescending = $sortedCountriesDescending") + return sortedCountriesDescending + } + + fun checkOneElementInList(): Boolean { + return countries.contains("Germany") + } + + fun checkOneElementInListUsingOperator(): Boolean { + return "Spain" in countries + } + + fun checkElementsInList(): Boolean { + return cities.containsAll(listOf("Calcutta", "Sao Paulo", "Sydney")) + } } \ 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 f4a7be0ec0..541c5fd64f 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.assertFalse import kotlin.test.assertNull import kotlin.test.assertTrue @@ -30,4 +31,78 @@ class ListExampleUnitTest { fun whenRetrieveElementsFirstAndLast_thenSuccess() { assertEquals("Australia", classUnderTest.retrieveElementsFirstAndLast()) } + + @Test + fun whenRetrieveSubList_thenSuccess() { + assertEquals(3, classUnderTest.retrieveSubList().size) + } + + @Test + fun whenRetrieveListSliceUsingIndices_thenSuccess() { + assertEquals(4, classUnderTest.retrieveListSliceUsingIndices().size) + } + + @Test + fun whenRetrieveListSliceUsingIndicesList_thenSuccess() { + assertEquals(2, classUnderTest.retrieveListSliceUsingIndicesList().size) + } + + @Test + fun whenCountList_thenSuccess() { + assertEquals(5, classUnderTest.countList()) + } + + @Test + fun whenCountListUsingPredicate_thenSuccess() { + assertEquals(3, classUnderTest.countListUsingPredicate()) + } + + @Test + fun whenCountListUsingProperty_thenSuccess() { + assertEquals(5, classUnderTest.countListUsingProperty()) + } + + @Test + fun whenAddToList_thenSuccess() { + assertEquals(11, classUnderTest.addToList().count()) + } + + @Test + fun whenRemoveFromList_thenSuccess() { + val list = classUnderTest.removeFromList() + assertEquals(3, list.size) + assertEquals("Sao Paulo", list[1]) + } + + @Test + fun whenReplaceFromList_thenSuccess() { + val list = classUnderTest.replaceFromList() + assertEquals(5, list.size) + assertEquals("Barcelona", list[1]) + } + + @Test + fun whenSortMutableList_thenSuccess() { + assertEquals("Sydney", classUnderTest.sortMutableList()[0]) + } + + @Test + fun whenSortList_thenSuccess() { + assertEquals("India", classUnderTest.sortList()[1]) + } + + @Test + fun whenCheckOneElementInList_thenSuccess() { + assertTrue(classUnderTest.checkOneElementInList()) + } + + @Test + fun whenCheckOneElementInListUsingOperator_thenSuccess() { + assertFalse(classUnderTest.checkOneElementInListUsingOperator()) + } + + @Test + fun whenCheckElementsInList_thenSuccess() { + assertTrue(classUnderTest.checkElementsInList()) + } } \ No newline at end of file