🎓 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
In this article, we will discuss the difference between Collection and Collections in Java.
Java is well known for its comprehensive library, one of which is the Java Collections Framework. It includes interfaces and classes that are used to represent and manipulate collections of objects. Two terms that are often discussed when talking about this framework are Collection and Collections. They sound similar, but they are fundamentally different.
Collection - It is an Interface
The Collection is an interface in the Java Collections Framework.
It defines the basic methods that all collections will have, such as add(), remove(), size(), isEmpty(), iterator(), etc.
The Collection is essentially a group of individual objects represented as a single unit.
It acts as the root interface in the collection hierarchy and is located in java.util package.
Collection is more of a concept for storing data, rather than a tool for manipulating data.
Collection Interface Example
import java.util.ArrayList;
import java.util.Collection;
public class CollectionDemo {
public static void main(String[] args) {
Collection < String > fruitCollection = new ArrayList < > ();
fruitCollection.add("banana");
fruitCollection.add("apple");
fruitCollection.add("mango");
System.out.println(fruitCollection);
fruitCollection.remove("banana");
System.out.println(fruitCollection);
System.out.println(fruitCollection.contains("apple"));
fruitCollection.forEach((element) -> {
System.out.println(element);
});
fruitCollection.clear();
System.out.println(fruitCollection);
}
}
Output:
[banana, apple, mango]
[apple, mango]
true
apple
mango
[] Collection <String> fruitCollection = new ArrayList<>();Collections - It is a Utility Class
Collections Utility Class Example
import java.util.*;
public class Main {
public static void main(String[] args) {
List<String> list = new LinkedList<>();
list.add("element 2");
list.add("element 1");
list.add("element 4");
list.add("element 3");
// Sorts the specified list into ascending order, according to
// the natural ordering of its elements.
Collections.sort(list);
for (String str : list) {
System.out.println("sort elements in ascending order --" + str);
}
}
} sort elements in ascending order --element 1
sort elements in ascending order --element 2
sort elements in ascending order --element 3
sort elements in ascending order --element 4Collection vs Collections - Cheat Sheet
Conclusion
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