Collections

What are collections?

Collections are any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the “Collection Framework” has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. It works as an interface that defines the highest-level of shared collection behavior, and extends Iterable which allows usage of ForEach loop.

How are collections used?

Collections can be used in the following ways

Types of Collections

  • List:
    • Ordered collection
    • Allows duplicate elements
    • Example: ArrayList and LinkedList
  • Set:
    • Unordered collection
    • No duplicates
    • Example: HashSet and TreeSet
  • Map:
    • Key-pair values
    • Each key must be unique
    • Example: HashMap and TreeMap
  • Queue:
    • Follows First-In-First-Out order
    • Example: LinkedList and PriorityQueue
  • Deque:
    • Double ended queue
    • Insertion and removal at both ends
    • Example: ArrayDeque

Popcorn Hack!

Come up with a real world example in which collections are used! Write your answer below: