Added functions for list write and sort operations

This commit is contained in:
Anirban Chatterjee 2020-06-04 18:07:25 +02:00
parent ba924ce978
commit db69599998
2 changed files with 170 additions and 1 deletions

View File

@ -84,5 +84,99 @@ class ListExample {
return countries.firstOrNull { it.length > 8 }
}
//5. Retrieve parts of the list
fun retrieveSubList(): List<String> {
val subList = countries.subList(1, 4)
println(subList)
return subList
}
fun retrieveListSliceUsingIndices(): List<String> {
val sliceList = countries.slice(1..4)
println(sliceList)
return sliceList
}
fun retrieveListSliceUsingIndicesList(): List<String> {
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<String> {
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<String> {
cities.remove("Seoul")
println(cities)
cities.removeAt(1)
println(cities)
return cities
}
fun replaceFromList(): List<String> {
cities.set(3, "Prague")
println(cities)
cities[4] = "Moscow"
println(cities)
cities.fill("Barcelona")
println(cities)
return cities
}
fun sortMutableList(): List<String> {
cities.sort()
println(cities)
cities.sortDescending()
println(cities)
return cities
}
fun sortList(): List<String> {
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"))
}
}

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