DEV Community

Cover image for Java Collections Framework (JCF) for Beginners
arun Kumar
arun Kumar

Posted on

Java Collections Framework (JCF) for Beginners

If you're learning Java, you've probably heard about Collections. They are one of the most important concepts in Java because they help us store and manage multiple objects easily.

In this blog, let's understand Java Collections in a simple way.

** What is Java Collection?**

The Java Collections Framework (JCF) is a set of classes and interfaces that helps us store and manipulate groups of objects.

Instead of using arrays with a fixed size, collections can grow or shrink dynamically.

** Why Use Collections?**

  • Store multiple objects
  • Dynamic size
  • Easy to add and remove data
  • Built-in methods for searching and sorting
  • Reduces coding effort

** Collection Hierarchy**

Collection
│
├── List
│   ├── ArrayList
│   ├── LinkedList
│   └── Vector
│
├── Set
│   ├── HashSet
│   ├── LinkedHashSet
│   └── TreeSet
│
└── Queue
    └── PriorityQueue

Map (Separate Interface)
├── HashMap
├── LinkedHashMap
└── TreeMap
Enter fullscreen mode Exit fullscreen mode

Note: Map is not part of the Collection interface.


** 1. List**

A List stores elements in order and allows duplicate values.

** Example**

Enter fullscreen mode Exit fullscreen mode


java
import java.util.*;

public class Main {
public static void main(String[] args) {

    List<String> list = new ArrayList<>();

    list.add("Java");
    list.add("Python");
    list.add("Java");

    System.out.println(list);
}
Enter fullscreen mode Exit fullscreen mode

}


**Output**

Enter fullscreen mode Exit fullscreen mode

[Java, Python, Java]

Enter fullscreen mode Exit fullscreen mode

** 2. Set**

A Set stores only unique elements.

Enter fullscreen mode Exit fullscreen mode


java
HashSet set = new HashSet<>();

set.add("Java");
set.add("Python");
set.add("Java");

System.out.println(set);


**Output**

Enter fullscreen mode Exit fullscreen mode

[Java, Python]

Enter fullscreen mode Exit fullscreen mode

** 3. Queue**

A Queue follows the FIFO (First In, First Out) rule.

Enter fullscreen mode Exit fullscreen mode


java
PriorityQueue queue = new PriorityQueue<>();

queue.add(30);
queue.add(10);
queue.add(20);

System.out.println(queue.poll());


**Output**

Enter fullscreen mode Exit fullscreen mode

10


** 4. Map**

A **Map** stores data as **key-value pairs**.

Enter fullscreen mode Exit fullscreen mode

``java
HashMap map = new HashMap<>();

map.put(1, "Java");
map.put(2, "Python");

System.out.println(map);
`

`

** Common Collection Classes**

Class Description
ArrayList Dynamic array
LinkedList Fast insertion and deletion
HashSet Stores unique elements
TreeSet Stores unique elements in sorted order
HashMap Stores key-value pairs
TreeMap Stores sorted key-value pairs

** Quick Comparison**

Collection Allows Duplicates Maintains Order
List ✅ Yes ✅ Yes
Set ❌ No Depends on implementation
Queue ✅ Yes FIFO
Map Keys ❌, Values ✅ Depends on implementation

** Conclusion**

The Java Collections Framework makes it easy to work with groups of objects. Remember these four main interfaces:

  • List → Ordered and allows duplicates.
  • Set → Stores unique elements.
  • Queue → Follows FIFO.
  • Map → Stores key-value pairs.

Once you understand these basics, learning advanced Java becomes much easier.

Top comments (0)