DEV Community

Pavithraarunachalam
Pavithraarunachalam

Posted on

Collections in Java

what is collection in java?

  • A Collection in Java is like a container that is used to store a group of objects together and perform operations like adding, removing, searching, and sorting easily.
  • It is part of the Java Collection Framework in the java.util package.

Key Points:

  • Present in java.util package.

  • Dynamic size → can grow or shrink.

  • Built-in methods → add, remove, search, sort, etc.

  • Supports interfaces: List, Set, Queue, Map.

Main Parts of the Java Collection Framework:

  • Interface
  • Implementation class
  • Algorithms
  • Iterators

1. Interfaces:

They provide the blueprint for collections.

Main interfaces:

Collection (root interface)

List → Ordered, allows duplicates

Set → No duplicates

Queue → FIFO (First In First Out)

Map (separate, stores Key–Value pairs)

2. Implementations (Classes):

These are the actual classes that implement the interfaces.

Interface Common Classes

List - ArrayList, LinkedList, Vector, Stack

Set - HashSet, LinkedHashSet, TreeSet

Queue - PriorityQueue, LinkedList, ArrayDeque

Map - HashMap, LinkedHashMap, TreeMap, Hashtable

3. Algorithms:

Utility methods provided by the Collections class:

  • Sorting → Collections.sort(list)

  • Searching → Collections.binarySearch(list, key)

  • Shuffling → Collections.shuffle(list)

  • Reverse → Collections.reverse(list)

4. Iterators:

Used to traverse elements in a collection:

  • Iterator → Basic iterator, forward direction.

  • ListIterator → Forward & backward traversal.

  • Enumeration → Legacy iterator for older collections.

Top comments (0)