🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
Introduction
In Kotlin, the Collection interface represents a generic collection of elements. It is a part of the kotlin.collections package and serves as the root of the collection hierarchy. The Collection interface is read-only, meaning it provides operations for querying the collection but does not allow modifying it.
Table of Contents
- What is the
CollectionInterface? - Key Functions of the
CollectionInterface - Implementations of the
CollectionInterface - Examples of
CollectionInterface Usage - Real-World Use Case
- Conclusion
1. What is the Collection Interface?
The Collection interface in Kotlin is a generic interface that defines a read-only collection of elements. It provides basic operations such as checking the size of the collection, checking if it contains a specific element, and iterating over the elements.
Syntax
interface Collection<out E> : Iterable<E>
2. Key Functions of the Collection Interface
The Collection interface provides several functions for interacting with collections:
size: Returns the number of elements in the collection.isEmpty(): Checks if the collection is empty.contains(element: @UnsafeVariance E): Checks if the collection contains the specified element.containsAll(elements: Collection<@UnsafeVariance E>): Checks if the collection contains all elements in the specified collection.iterator(): Returns an iterator over the elements in the collection.
3. Implementations of the Collection Interface
The Collection interface has several implementations in Kotlin, including:
List: An ordered collection of elements.Set: A collection of unique elements.Map: A collection of key-value pairs (though it doesn't directly implementCollection).
4. Examples of Collection Interface Usage
Example 1: Basic Usage of Collection
This example demonstrates the basic usage of the Collection interface using a List implementation.
fun main() {
val collection: Collection<String> = listOf("Apple", "Banana", "Cherry")
println("Collection: $collection") // Output: Collection: [Apple, Banana, Cherry]
println("Size: ${collection.size}") // Output: Size: 3
println("Is empty: ${collection.isEmpty()}") // Output: Is empty: false
println("Contains 'Banana': ${collection.contains("Banana")}") // Output: Contains 'Banana': true
println("Contains all ['Apple', 'Cherry']: ${collection.containsAll(listOf("Apple", "Cherry"))}") // Output: Contains all ['Apple', 'Cherry']: true
}
Example 2: Iterating Over a Collection
This example demonstrates how to iterate over a collection using the iterator() function.
fun main() {
val collection: Collection<Int> = setOf(1, 2, 3, 4, 5)
val iterator = collection.iterator()
while (iterator.hasNext()) {
val element = iterator.next()
println(element)
}
}
Output:
1
2
3
4
5
Explanation:
This example shows how to iterate over the elements in a collection using an iterator.
Example 3: Using contains and containsAll
This example demonstrates how to use the contains and containsAll functions.
fun main() {
val collection: Collection<String> = listOf("Kotlin", "Java", "Python")
println("Contains 'Java': ${collection.contains("Java")}") // Output: Contains 'Java': true
println("Contains all ['Kotlin', 'Python']: ${collection.containsAll(listOf("Kotlin", "Python"))}") // Output: Contains all ['Kotlin', 'Python']: true
}
Output:
Contains 'Java': true
Contains all ['Kotlin', 'Python']: true
Example 4: Checking Size and Emptiness
This example demonstrates how to check the size of a collection and if it is empty.
fun main() {
val collection: Collection<Double> = listOf(1.1, 2.2, 3.3)
println("Size of collection: ${collection.size}") // Output: Size of collection: 3
println("Is collection empty: ${collection.isEmpty()}") // Output: Is collection empty: false
}
Output:
Size of collection: 3
Is collection empty: false
5. Real-World Use Case: Filtering a Collection
You can use the Collection interface to filter elements from a collection based on certain criteria.
Example: Filtering Even Numbers
fun main() {
val numbers: Collection<Int> = listOf(1, 2, 3, 4, 5, 6)
val evenNumbers = numbers.filter { it % 2 == 0 }
println("Even numbers: $evenNumbers") // Output: Even numbers: [2, 4, 6]
}
Output:
Even numbers: [2, 4, 6]
Explanation:
This example uses the filter function to create a new list containing only the even numbers from the original collection.
Conclusion
The Collection interface in Kotlin is a powerful and flexible way to handle read-only collections of elements. It is part of the kotlin.collections package and provides essential operations for querying collections. Understanding and utilizing the Collection interface can greatly enhance your ability to work with collections in Kotlin.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment