chevron-up bell reply instagram twitter2 feed3 finder search-25px-p0

A comparison between Kotlin and Swift: Collections

2017-10-14

Similar to Swift, collection in Kotlin includes:

  • List
  • Set
  • Map

1. List

Features of List:

  • May have duplicate element
  • May hold different data types
  • All elements are unordered

Similar to array in swift, list can be immutable and mutable. With immutable list, it can't be added or removed.

[gist https://gist.github.com/arkilis/1cdabbd71f64cdde50637179157375b8]

In Swift, the mutable and immutable array is differentiated by using let and var. For example:

[gist https://gist.github.com/arkilis/53f00a2ba4087bc14492f268e3222542#file-swift_array-swift]

2. Set

  • Must have NO duplicate
  • May hold different data types
  • Unordered

[gist https://gist.github.com/arkilis/45c7904642fe82b78ed4f224c28fa581/]

Swift version:

[gist https://gist.github.com/arkilis/f421cd6c01726354967e8da9ecf78964/]

3. Map

Equivalent to dictionary

  • Key-Value pair
  • No duplicate

[gist https://gist.github.com/arkilis/4342a9c1ba5fdfce5edf781c242912fe]

Swift version:

[gist https://gist.github.com/arkilis/1fe799fcb1badbfaddac3b48c473b81b]


Reference

http://www.arkilis.me/comparison-kotlin-swift

arkilis

Comments: