Skip to content

Having fun writing and exploring collection classes and their usages.

License

Notifications You must be signed in to change notification settings

basecs101/java-collection-concepts-2

Repository files navigation

java-collection-concepts-2

Having fun writing and exploring collection classes and their usages.

  1. The Stack class represents a last-in-first-out (LIFO) stack of objects. It extends class Vector with five operations that allow a vector to be treated as a stack. The usual push and pop operations are provided, as well as a method to peek at the top item on the stack, a method to test for whether the stack is empty, and a method to search the stack for an item and discover how far it is from the top. When a stack is first created, it contains no items.

    A more complete and consistent set of LIFO stack operations is provided by the Deque interface and its implementations, which should be used in preference to this class. For example: Deque stack = new ArrayDeque();

    1. PriorityQueue (implements Queue)

    2. ArrayDeque (implements Deque and Deque extends Queue)

    3. LinkedList (implements Deque and Deque extends Queue)

  2. List

  3. Set

    1. TreeSet

  4. Map

  5. HashTable

  6. Vector

    The Vector class implements a growable array of objects. Like an array, it contains components that can be accessed using an integer index. However, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created.

    Each vector tries to optimize storage management by maintaining a capacity and a capacityIncrement. The capacity is always at least as large as the vector size; it is usually larger because as components are added to the vector, the vector's storage increases in chunks the size of capacityIncrement. An application can increase the capacity of a vector before inserting a large number of components; this reduces the amount of incremental reallocation.

    As of the Java 2 platform v1.2, this class was retrofitted to implement the List interface, making it a member of the Java Collections Framework. Unlike the new collection implementations, Vector is synchronized. If a thread-safe implementation is not needed, it is recommended to use ArrayList in place of Vector

  7. Collections.sort(List list) and Arrays.sort()

  8. Comparator and Comparable Interfaces and their differences

  9. Immutable Collections in Java 9

    1. List.of() --> returns immutable List in terms of size , remove and add not allowed also set(replace) not allowed

    2. Arrays.asList() --> returns immutable List in terms of size, remove and add not allowed but set(replace) allowed Object state of each element of list can be updated in both the cases of list examples.

    > Use cases

     1. While passing list as param to a method that doesn't modify it.
     2. Use it when multiple threads are sharing the list and you want the same list to be used by all threads.
    

Collection flow diagram

Flow Diagram

About

Having fun writing and exploring collection classes and their usages.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages