collections-kotlin

Collections in Kotlin

Collections is nothing but a backpack, guess what are the things you can store in a backpack.

Probably you must have guessed everything that we can store in backpack, but let list few of them : 1. Laptop, Mouse, HeadPhones, earPhones, Mouse (computer's and animal too), Book, Notepad, pen, pencil you can write so on.

Almost you can store everything, you cannot store elephant though.

Within the backpack's size you can store all the things.

Lets' discuss about kotlin Collection : Collection is a framework which manipulates only one data type, which is god data type of Kotlin.

I mean data type of Any class, I hope you know that Any class is the top most class in Kotlin.

Collection framework mostly used along with group of Objects rather than Object.
<

Read Properties File in Kotlin

Description as per Books:

Collections in kotlin is a framework that provides an architecture to store and manipulate the group of objects Collections in Kotlin is a framework that provides an architecture to store and manipulate the group of objects

I am not going to discuss everything present in collection, but only collection that we use frequently and which are :

collections-kotlin
  • List : List is used to store group of objects/items, where the items can be duplicated. Stores the items in the given/insertion order
  • Set : Set is used to store a group of objects/items, But Set does not allow duplicate items.
  • Queue : Queue implementations imitate First-In, First-Out (FIFO) concept (insertion at the end, extraction at the front)
  • Dequeue : DeQueue implementations imitate First-In, First-Out (FIFO) and Last-In, First-Out concepts, insertion and extraction can happen at both the ends. This is nothing but Double ended Queue
  • Map : Map is used to store the objects/items in the form key value pairs, where the keys can't be duplicated but you can have duplicate values. Map is not a collection but mostly used along collections.

Kotlin's Primary Constructor

Benefits of Collections

  • Re-usability: Kotlin Collections Framework provides common classes and utility methods than can be used with different types of collections, Collections promotes the reusability of the code.
  • Quality: Kotlin Collection Framework improves the program quality, since the code is already tested and used by thousands of developers.
  • Speed: Most of the utility code is already present, so developer can focus only on their logic of business
  • Variety : Kotlin Collections can store any kind of data in their collections
  • No-Size Constraint : Unlike arrays , Collection do not have any limit on side and size can be increased dynamically

Inheritance in kotlin

Kotlin Collections

Kotlin does not have collections of its own, Kotlin depends solely on Java Collections Library.

Every collections in kotlin is instance of Java collection, Kotlin Collection provide few more extension methods to existing method in java.

Kotlin Provide more methods to perform operation more efficiently more than Java. I do not expect you to understand java to learn this collection, so do not worry if you do not know java.

More over you do not have to convert the Java to kotlin to use the collections, this makes easier an remove the overhead

Kotlin Enhances the Java Collections by distinguishing the read-only(mutable) and mutable collections

Mutable collections can be add, remove, update the values of an element/item in same collection and it will not create the new collections.

Immutable collections also provides same operation as Mutable Collections but immutable collections produces new collection when you make a change.

You can find all the collections in the kotlin.collections package.

Kotlin Generics with Classes

Difference between Arrays and Collections

  • Arrays are fixed in size but Collections are dynamic in size.
  • With respect to memory arrays are not good to use but with respect to memory Collections are better to use.
  • With respect to performance its better to use arrays but with respect to performance collection are not good to use.
  • Arrays can hold only homogeneous elements but collections can hold both homogeneous and heterogeneous elements
  • Arrays don't have ready made methods but collections have ready made data structures and methods.
  • Arrays can hold both primitives and wrapper objects but collections can hold only objects.

About Author

This article is written by Pavan, who is serving notice period in an MNC, Bangalore. He thought, let the articles speak rather than his image. He is also the same person who created the reporter for Protractor Jasmine

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions