Collections in Java

Collections in Java are built-in data structures that are used to store, manipulate, and process groups of objects. They provide a higher level of abstraction and offer a wide range of operations to perform on data, such as adding, removing, searching, sorting, and more. Java provides a comprehensive set of collection classes and interfaces that facilitate efficient and organized handling of data.

Here are some commonly used collection types in Java:

List

A List is an ordered collection that allows duplicate elements. It maintains the insertion order of elements.

Syntax
List< Type > list = new ArrayList<>();
Example
public class CollectionExamples {
    public static void main(String[] args) {
        // List
        List names = new ArrayList<>();
        names.add("Alice");
        names.add("Bob");
        names.add("Charlie");
        System.out.println(names);  // Output: [Alice, Bob, Charlie]
    }
}

Set

A Set is a collection that does not allow duplicate elements. It does not maintain any particular order of elements.

Syntax
Set set = new HashSet<>();
Example
public class CollectionExamples {

    public static void main(String[] args) {
        // Set
        Set numbers = new HashSet<>();
        numbers.add(1);
        numbers.add(2);
        numbers.add(3);
        numbers.add(3);  // Duplicate elements are ignored
        System.out.println(numbers);  // Output: [1, 2, 3]
    }
}

Map

A Map is an object that maps keys to values. Each key in a Map must be unique, and it allows retrieval of values based on the keys.

Syntax
Map map = new HashMap<>();
Example
public class CollectionExamples {

    public static void main(String[] args) {
        // Map
        Map ages = new HashMap<>();
        ages.put("Alice", 25);
        ages.put("Bob", 30);
        ages.put("Charlie", 35);
        System.out.println(ages.get("Bob"));  // Output: 30
    }
}

Queue

A Queue is a collection that follows the First-In-First-Out (FIFO) principle. Elements are added at the end and removed from the beginning.

Syntax
Queue queue = new LinkedList<>();
Example
public class CollectionExamples {

    public static void main(String[] args) {
        // Queue
        Queue tasks = new LinkedList<>();
        tasks.add("Task 1");
        tasks.add("Task 2");
        tasks.add("Task 3");
        System.out.println(tasks.poll());  // Output: Task 1 (removes and returns the head)
    }
}

Stack

A Stack is a collection that follows the Last-In-First-Out (LIFO) principle. Elements are added and removed from the top.

Syntax
Stack stack = new Stack<>();
Example
Stack< String > books = new Stack<>();
books.push("Book 1");
books.push("Book 2");
books.push("Book 3");
System.out.println(books.pop());  // Output: Book 3 (removes and returns the top)
Deque

A Deque (Double Ended Queue) is a collection that allows insertion and removal of elements from both ends.

Syntax
Deque deque = new ArrayDeque<>();
Example
public class CollectionExamples {

    public static void main(String[] args) {
   Deque deque = new ArrayDeque<>();
        deque.addFirst(1);
        deque.addLast(2);
        deque.addLast(3);
        System.out.println(deque.pollFirst());  // Output: 1 (removes and returns the first element)
    }
}