bad programmer and a good one is whether he considers his code or his data structures more important. Bad programmers worry about the code. Good programmers worry about data structures and their relationships.” 3
elements. ◎ A mutable interface that extends the corresponding read-only interface with write operations : removing, adding, updating... 5 Collection Type Interfaces
Elements can occur more than once. (Group of words) ◎ Set: collection of unique elements (a group of objects without repetitions). (Alphabet) 6 Collection Types
// Not Recommended eventWordList[0] // getting element with index eventWordList.indexOf(kotlinLanguage) // getting index of element eventWordList.shuffled() // Doesn't effect original collection
eventWordList.set(0,"Java :(") // Not Recommended :) eventWordList[0] = kotlinLanguage // Updating with index eventWordList.removeAt(0) // Deleting with index
= eventWordList - exclamationMark // remove first occurrence // [Kotlin, Everywhere, is, a, great, event] val removedWithList = eventWordList - listOf("is","a") // remove all occurrences // [Kotlin, Everywhere, great, event, !] val addedList = eventWordList + "Let's fun with it!" // [Kotlin, Everywhere, is, a, great, event, !, Let's fun with it!]
names.last() // Okan names.elementAt(2) // Elif names.elementAtOrNull(4) // prevent IndexOutOfBoundException - returns null names.elementAtOrElse(4) { // Do something because of index not found }
} // Murat names.last { it.contains("f") } // Elif names.firstOrNull { // You can use also find() alias it.startsWith("z") } // Null names.lastOrNull { //You can use also findLast() alias it.endsWith("n") } // Okan
", "great ", "event", "!") eventWordList.reduce { sentence, element -> sentence + element } // Kotlin Everywhere is a great event! eventWordList.fold("") { sentence, element -> sentence + element } // Kotlin Everywhere is a great event!
given predicate. none() returns true if none of the elements match the given predicate. all() returns true if all elements match the given predicate. Note that all() returns true when called with any valid predicate on an empty collection 30 Testing Predicates
= (tvSeries union setOf("Game Of Thrones","Lost")).toMutableSet() // Lost is not added again // [Breaking Bad, How I Met Your Mother?, Lost, Game Of Thrones] val badFinals = mutableSetOf("Game Of Thrones","Lost") tvSeries = (tvSeries - badFinals) as MutableSet<String> // [Breaking Bad, How I Met Your Mother?]
to "Trabzon") cityMap.get(34) // get with function - Not Recommended cityMap[34] // get with index cityMap.keys // get all keys cityMap.values // get all values
to "Trabzon") cityMap.put(16, "Bursa") // put with function - not recommended cityMap[35] = "İzmir" // put-update with index cityMap.remove(34) // delete cityMap.getOrPut(28,{ "Giresun" }) // if there is a value with key 28, get it otherwise put the value with key 28 // {6=Ankara, 61=Trabzon, 16=Bursa, 35=İzmir, 28=Giresun} cityMap.getOrDefault(34,"Not Found!")
requested by consumer yield(0) // return element to sequence consumer yieldAll(listOf(2, 4)) // can take any Iterable or Sequence yieldAll(generateSequence(6) { it + 2 }) // infinite call // - it must be last - // all subsequent calls will never be executed } evenNumbers.take(5).toList() // [0, 2, 4, 6, 8]
Iterable but implement another approach to multi-step collection processing. • When the processing of an Iterable each process step completes and returns its result – an intermediate collection. In turn, multi- step processing of sequences is executed lazily when possible 45
different as well: Sequence performs all the processing steps one-by-one for every single element. In turn, Iterable completes each step for the whole collection and then proceeds to the next step. 46