Skip to content

Latest commit

 

History

History
128 lines (109 loc) · 6.48 KB

List, Set, Map, Queue, Stack, Iterator.md

File metadata and controls

128 lines (109 loc) · 6.48 KB

In Java, the collections framework provides various types of data structures to store and manipulate data. Here is a detailed explanation of each of the following Java collection types: List, Set, Map, Queue, Stack, and Iterator.

1. List

  • Definition: A List is an ordered collection of elements that allows duplicates. The elements in a List are indexed, and you can access them by their position in the list (index).

  • Key Features:

    • Ordering: The elements in a List are ordered, meaning they maintain the sequence of insertion.
    • Duplicates: Lists allow duplicate elements.
    • Indexing: Each element in the List can be accessed using its index (position in the List).
  • Common Implementations:

    • ArrayList: A resizable array implementation of the List interface. It allows fast random access but slower insertions and deletions (except at the end).
    • LinkedList: A doubly-linked list implementation that allows for efficient insertions and deletions but slower access to elements compared to ArrayList.
    • Vector: Similar to ArrayList, but it is synchronized and thread-safe, making it less efficient for single-threaded applications.
  • Example:

    List<String> list = new ArrayList<>();
    list.add("Apple");
    list.add("Banana");
    list.add("Apple");
    System.out.println(list);  // Output: [Apple, Banana, Apple]

2. Set

  • Definition: A Set is an unordered collection of elements that does not allow duplicates. It does not guarantee any particular order of elements.

  • Key Features:

    • No Duplicates: A Set does not allow duplicate elements.
    • Unordered: The elements in a Set are not stored in any particular order.
    • Efficiency: Sets are efficient for operations like membership tests (checking if an element exists in the Set).
  • Common Implementations:

    • HashSet: This is the most commonly used implementation. It uses a hash table and does not guarantee any specific order.
    • LinkedHashSet: Similar to HashSet, but it maintains the insertion order.
    • TreeSet: A Set that stores elements in a sorted order, based on the natural ordering of the elements or a specified comparator.
  • Example:

    Set<String> set = new HashSet<>();
    set.add("Apple");
    set.add("Banana");
    set.add("Apple");
    System.out.println(set);  // Output: [Apple, Banana]

3. Map

  • Definition: A Map is an object that maps keys to values. A Map does not allow duplicate keys, but it can have multiple values associated with different keys.

  • Key Features:

    • Key-Value Pairs: Each element is a key-value pair.
    • No Duplicate Keys: A Map cannot have duplicate keys, but different keys can have the same value.
    • Efficient Search: Maps provide efficient search, insertion, and deletion operations.
  • Common Implementations:

    • HashMap: This implementation stores the key-value pairs in a hash table and provides constant-time performance for basic operations (like get and put).
    • LinkedHashMap: Similar to HashMap but maintains the insertion order of keys.
    • TreeMap: A Map implementation that stores keys in a sorted order.
    • Hashtable: An older version of HashMap, which is synchronized and thread-safe.
  • Example:

    Map<String, Integer> map = new HashMap<>();
    map.put("Apple", 1);
    map.put("Banana", 2);
    map.put("Apple", 3);  // Updates value for "Apple"
    System.out.println(map);  // Output: {Apple=3, Banana=2}

4. Queue

  • Definition: A Queue is a collection used to hold elements before processing them. It follows the FIFO (First-In-First-Out) principle, meaning the first element added to the queue will be the first to be removed.

  • Key Features:

    • FIFO: The order of elements in a Queue is such that the element that was added first is processed first.
    • Offer and Poll: Instead of using add and remove, queues typically use offer (for adding elements) and poll (for removing elements).
  • Common Implementations:

    • LinkedList: Implements the Queue interface and can be used as a Queue.
    • PriorityQueue: A queue that processes elements based on their priority rather than the order they were added.
    • ArrayDeque: A resizable array implementation of the Deque interface (which is a double-ended queue).
  • Example:

    Queue<String> queue = new LinkedList<>();
    queue.offer("Apple");
    queue.offer("Banana");
    System.out.println(queue.poll());  // Output: Apple
    System.out.println(queue);  // Output: [Banana]

5. Stack

  • Definition: A Stack is a collection that follows the LIFO (Last-In-First-Out) principle. The last element added to the stack is the first one to be removed.

  • Key Features:

    • LIFO: The last element added is the first one to be removed.
    • Push and Pop: Elements are added to the stack using push and removed using pop.
  • Common Implementations:

    • Stack: A class that implements a stack using a vector. It is considered somewhat outdated, and modern implementations often use Deque or LinkedList for stack-like behavior.
  • Example:

    Stack<String> stack = new Stack<>();
    stack.push("Apple");
    stack.push("Banana");
    System.out.println(stack.pop());  // Output: Banana
    System.out.println(stack);  // Output: [Apple]

6. Iterator

  • Definition: An Iterator is an object that allows you to traverse through the elements of a collection (such as a List, Set, etc.) one by one. It provides methods to iterate over the collection.

  • Key Features:

    • Iteration: It allows you to iterate over the collection without exposing the internal structure of the collection.
    • Remove: The Iterator interface allows for safe removal of elements while iterating.
  • Methods:

    • hasNext(): Returns true if there are more elements in the collection.
    • next(): Returns the next element in the collection.
    • remove(): Removes the last element returned by the iterator.
  • Example:

    List<String> list = new ArrayList<>();
    list.add("Apple");
    list.add("Banana");
    Iterator<String> iterator = list.iterator();
    while (iterator.hasNext()) {
        System.out.println(iterator.next());
    }

These data structures and interfaces are fundamental in Java programming. Depending on the problem, you can choose the appropriate collection type based on whether you need ordering, uniqueness, key-value mapping, or specific operations like push/pop or FIFO/LIFO handling.