Skip to content

Commit 0811b20

Browse files
authored
Merge pull request eugenp#6494 from Maiklins/BAEL-2217
Bael 2217
2 parents 53f4bbf + 8cf72e2 commit 0811b20

1 file changed

Lines changed: 26 additions & 0 deletions

File tree

  • core-kotlin/src/main/kotlin/com/baeldung/forEach

‎core-kotlin/src/main/kotlin/com/baeldung/forEach/forEach.kt‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,15 @@ class Country(val name : String, val cities : List<City>)
55

66
class City(val name : String, val streets : List<String>)
77

8+
fun City.getStreetsWithCityName() : List<String> {
9+
return streets.map { "$name, $it" }.toList()
10+
}
11+
12+
fun Country.getCitiesWithCountryName() : List<String> {
13+
return cities.flatMap { it.getStreetsWithCityName() }
14+
.map { "$name, $it" }
15+
}
16+
817
class World {
918

1019
private val streetsOfAmsterdam = listOf("Herengracht", "Prinsengracht")
@@ -45,6 +54,19 @@ class World {
4554
}
4655
}
4756
}
57+
58+
fun allStreetsFlatMap() {
59+
60+
countries.flatMap { it.cities}
61+
.flatMap { it.streets}
62+
.forEach { println(it) }
63+
}
64+
65+
fun allFlatMapTable() {
66+
67+
countries.flatMap { it.getCitiesWithCountryName() }
68+
.forEach { println(it) }
69+
}
4870
}
4971

5072
fun main(args : Array<String>) {
@@ -56,6 +78,10 @@ fun main(args : Array<String>) {
5678
world.allNested()
5779

5880
world.allTable()
81+
82+
world.allStreetsFlatMap()
83+
84+
world.allFlatMapTable()
5985
}
6086

6187

0 commit comments

Comments
 (0)