Collections in JAVA.

 Collections in Java are part of the Java Collections Framework, which provides a set of classes and interfaces for storing and manipulating groups of objects. Collections simplify the handling of data structures like lists, sets, queues, and maps by offering ready-to-use methods for common operations such as searching, sorting, inserting, and deleting elements.

Key Interfaces in the Java Collections Framework

  1. Collection: The root interface for all collection classes.

    • Subinterfaces: List, Set, Queue, etc.
  2. List:

    • Ordered collection of elements.
    • Allows duplicate elements.
    • Common implementations:
      • ArrayList (dynamic array-based implementation).
      • LinkedList (doubly linked list implementation).
      • Vector (synchronized array-based implementation).
  3. Set:

    • Collection that does not allow duplicate elements.
    • Common implementations:
      • HashSet (unordered, backed by a hash table).
      • LinkedHashSet (maintains insertion order).
      • TreeSet (sorted, backed by a red-black tree).
  4. Queue:

    • Used to hold elements prior to processing.
    • Follows FIFO (First In, First Out) in most cases.
    • Common implementations:
      • PriorityQueue (elements sorted by natural ordering or a comparator).
      • LinkedList (also implements the Queue interface).
  5. Deque:

    • Double-ended queue that allows insertion and removal from both ends.
    • Common implementations:
      • ArrayDeque.
      • LinkedList.
  6. Map (Not a part of Collection interface but part of the framework):

    • Maps keys to values.
    • Does not allow duplicate keys.
    • Common implementations:
      • HashMap (unordered, backed by a hash table).
      • LinkedHashMap (maintains insertion order).
      • TreeMap (sorted, backed by a red-black tree).
      • Hashtable (synchronized implementation).



Important Classes in Java Collections Framework

  1. Collections: A utility class with static methods for operating on collections, like sort, reverse, shuffle, binarySearch, etc.

  2. Arrays: Utility class for operations on arrays, such as sort, binarySearch, and converting arrays to lists.


Example Code

Here’s an example of using some common collection classes:

java
import java.util.*; public class CollectionExample { public static void main(String[] args) { // List example List<String> list = new ArrayList<>(); list.add("Apple"); list.add("Banana"); list.add("Apple"); System.out.println("List: " + list); // Set example Set<String> set = new HashSet<>(list); System.out.println("Set: " + set); // Queue example Queue<String> queue = new LinkedList<>(); queue.add("First"); queue.add("Second"); queue.add("Third"); System.out.println("Queue: " + queue); System.out.println("Removed from queue: " + queue.poll()); // Map example Map<Integer, String> map = new HashMap<>(); map.put(1, "One"); map.put(2, "Two"); map.put(1, "Updated One"); System.out.println("Map: " + map); } }

Benefits of Java Collections Framework

  1. Reusable and efficient: Pre-built data structures save development time.
  2. Interoperability: Common interfaces make switching implementations easy.
  3. Dynamic nature: Collections can grow or shrink dynamically compared to arrays.
  4. Built-in algorithms: Sorting, searching, and other utility methods are provided.

Comments