Part2 Ch2.1 Collections
- Collection type : List, Set, Map
- read-only interface
- mutable interface
-> extends the corresponding read-only interface with write operations
-> adding, removing, updating elements 가능
val numbers = mutableListOf("one", "two", "three", "four")
numbers.add("five")
List< T >
- stores elements in a specified order
- provides indexed access ( zero ~ (list.size - 1))
- an array's size is defined upon initialization and is never changed
- in turn, a list doesn't have a predefined size
-> can be changed as a result of write operations
Set< T >
- stores unique elements, their order is generally undefined
- The default implementation of Set: LinkedHashSet
–> preserves the order of elements insertion
-> Hence, the functions that rely on the order, such as first() or last(), return predictable results on such sets
- An alternative implementation: HashSet
–> says nothing about the elements order
-> so calling such functions on it returns unpredictable results
-> HashSet requires less memory to store the same number of elements.
Map< K, V >
- stores key-value pairs
- keys are unique, but different keys can be paired with equal values
- The default implementation of Map: LinkedHashMap
–> preserves the order of elements insertion
- An alternative implementation: HashMap
–> says nothing about the elements order
📌참고자료