codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

50 / 65 topics
50Java Collections51Java ArrayList52Java LinkedList53Java HashSet54Java HashMap55Java Iterator
Tutorials/Java Programming/Java Collections
☕Java Programming

Java Collections

Updated 2026-05-12
25 min read

Java Collections

In the world of programming, managing data efficiently is crucial. Java provides a robust set of tools to handle collections of objects, collectively known as the Java Collections Framework. This framework offers a wide range of interfaces and classes that simplify tasks such as storing, retrieving, manipulating, and processing groups of objects.

Introduction

The Java Collections Framework is a comprehensive library that provides a unified architecture for storing and manipulating groups of objects. It includes various data structures like lists, sets, queues, maps, and more. Understanding the core components of this framework—such as the Collection interface and the Iterable interface—is essential for writing efficient and effective Java programs.

Core Content

Collections Framework Overview

The Java Collections Framework is a set of classes and interfaces that implement commonly reusable collection data structures. It provides a high-level abstraction to store, retrieve, manipulate, and communicate aggregate data. The framework is organized into several sub-packages under java.util, including:

  • Interfaces: Define the basic behavior of collections.
  • Classes: Implement these interfaces with specific data structures.

The main interfaces in the Collections Framework are:

  • Collection
  • List
  • Set
  • Queue
  • Map

Collection Interface

The Collection interface is the root interface for all collection classes. It provides methods to perform operations on collections, such as adding, removing, and iterating over elements. Here's a brief overview of some key methods:

MethodDescription
add(E e)Adds an element to the collection.
remove(Object o)Removes an element from the collection.
size()Returns the number of elements in the collection.
isEmpty()Checks if the collection is empty.
iterator()Returns an iterator over the elements.

Example: Basic Collection Operations

Let's create a simple example to demonstrate basic operations on a collection.

CollectionExample.java
1import java.util.ArrayList;
2import java.util.Collection;
3
4public class CollectionExample {
5 public static void main(String[] args) {
6 // Create a collection
7 Collection<String> collection = new ArrayList<>();
8
9 // Add elements to the collection
10 collection.add("Apple");
11 collection.add("Banana");
12
13 // Print the size of the collection
14 System.out.println("Size: " + collection.size());
15
16 // Check if the collection is empty
17 System.out.println("Is Empty? " + collection.isEmpty());
18
19 // Iterate over the collection
20 for (String fruit : collection) {
21 System.out.println(fruit);
22 };
23
24 // Remove an element from the collection
25 collection.remove("Apple");
26
27 // Print the updated size of the collection
28 System.out.println("Updated Size: " + collection.size());
29 }
30}
Output
Size: 2
Is Empty? false
Apple
Banana
Updated Size: 1

Iterable Interface

The Iterable interface is a marker interface that indicates that the implementing class can be iterated over using a for-each loop. All collection classes in Java implement this interface, providing a common way to iterate over elements.

Example: Using Iterable with a Collection

Let's modify the previous example to demonstrate how the Iterable interface works.

IterableExample.java
1import java.util.ArrayList;
2import java.util.Collection;
3
4public class IterableExample {
5 public static void main(String[] args) {
6 // Create a collection
7 Collection<String> collection = new ArrayList<>();
8
9 // Add elements to the collection
10 collection.add("Apple");
11 collection.add("Banana");
12
13 // Iterate over the collection using an enhanced for loop
14 for (String fruit : collection) {
15 System.out.println(fruit);
16 }
17 }
18}
Output
Apple
Banana

Practical Example

Let's create a practical example that combines the use of Collection and Iterable interfaces to manage a list of books.

BookManager.java
1import java.util.ArrayList;
2import java.util.Collection;
3
4class Book {
5 private String title;
6 private String author;
7
8 public Book(String title, String author) {
9 this.title = title;
10 this.author = author;
11 }
12
13 @Override
14 public String toString() {
15 return "Book{" +
16 "title='" + title + ''' +
17 ", author='" + author + ''' +
18 '}';
19 }
20}
21
22public class BookManager {
23 private Collection<Book> books;
24
25 public BookManager() {
26 this.books = new ArrayList<>();
27 }
28
29 public void addBook(Book book) {
30 books.add(book);
31 }
32
33 public void removeBook(Book book) {
34 books.remove(book);
35 }
36
37 public void printBooks() {
38 for (Book book : books) {
39 System.out.println(book);
40 }
41 }
42
43 public static void main(String[] args) {
44 BookManager manager = new BookManager();
45 manager.addBook(new Book("1984", "George Orwell"));
46 manager.addBook(new Book("To Kill a Mockingbird", "Harper Lee"));
47
48 manager.printBooks();
49
50 manager.removeBook(new Book("1984", "George Orwell"));
51
52 System.out.println("
53After removing '1984':");
54 manager.printBooks();
55 }
56}
Output
Book{title='1984', author='George Orwell'}
Book{title='To Kill a Mockingbird', author='Harper Lee'}

After removing '1984':
Book{title='To Kill a Mockingbird', author='Harper Lee'}

Summary

  • Collections Framework: A comprehensive library for managing collections of objects.
  • Collection Interface: Provides basic methods to operate on collections.
  • Iterable Interface: Allows iteration over elements in a collection using a for-each loop.

What's Next?

In the next topic, we will dive deeper into one of the most commonly used classes in the Collections Framework: ArrayList. We'll explore its features, usage, and how it differs from other list implementations. Stay tuned!


PreviousBufferedReader & BufferedWriterNext Java ArrayList

Recommended Gear

BufferedReader & BufferedWriterJava ArrayList