Collections Framework

Advantage of Collections over Arrays

  • Collections are Grow-able in Nature
  • Collection can hold heterogeneous data type. (An Interface type of Collection can be created)
  • Collections are created based on some standard data structure

9 Key Interfaces of Collection Framwork

  1. Collection
  2. List
  3. Set
  4. SortedSet
  5. NavigableSet
  6. Queue
  7. Map
  8. SortedMap
  9. NavigableMap

Collection Interface

  • Most common methods applicable for almost all Interface is inside Collection Interface

Methods

  • Query Methods
    • size()
    • contains()
    • iterator()
    • toArray()
  • Modification Methods
    • add()
    • remove()
  • Bulk Operations
    • containsAll()
    • addAll()
    • removeAll()
    • removeIf()
    • retainAll()
    • clear()
  • Comparison and Hashing
    • equals()
    • hashcode()

Collection vs Collections

  • Collection is an Interface (Root interface of collections)
  • Collections is a Utility class used for collections. It contains method like searching and sorting
Collection interface - BenchResources.Net
  • Collection<I>
    • List<I>
      • ArrayList
      • LinkedList
      • Vector (Legacy)
        • Stack(Legacy)
    • Set<I>
      • HashSet
      • LinkedHashSet
      • SortedSet<I>
        • NavigableSet<I>
          • TreeSet
    • Queue<I>
      • Priority Queue
      • Blocking Queue<I>
        • PriorityBlockingQueue
        • LinkedBlockingQueue
  • Map<I>
    • HashMap
      • LinkedHashMap
    • WeakHashMap
    • IdentityHashMap
    • Hashtable(Legacy)
      • Properties(Legacy)
    • SortedMap<I>
      • NavigableMap<I>
        • TreeMap

Other Classes :

  • Sorting
    • Comparable<I>
    • Comparator<I>
  • Cursor
    • Enumeration(Legacy)
    • Iterator
    • ListIterator
  • Utility Classes
    • Collections
    • Arrays

Description :

  • The java collections Framework defines several classes and interfaces which can be used to represent a group of individual object as a single entity.
  • These classes and interface implement the collection data structures.
  • For Example : list, stack, queue or maps.
  • The collection framework was designed and developed primarily by Joshua Bloch
  • It was introduced in JDK 1.2
  • Why to use these collections?
    • We don’t have to implement every algorithm and data structure from the scratch, its already written into java.
    • It is already been tested.
  • Almost all Collections are derived from the java.utils.Collection interface
  • The main advantage of Collections over array is that collections are grow-able in size, i.e we need not know the size of elements in advance.
  • toArray() method can transform any collection into one dimensional array.
  • The Collection interface extends the java.lang.Iterable interface, this is why we can use for-each loop.

We write for each loop like below example :

for(String fruits : listOfFruits){
   System.out.println(fruits);
}

After compilation of .java file, .class file contains below code :

for (Iterator<String> i = listOfFruits.iterator(); i.hasNext();) {
   String fruits = i.next();
   System.out.println(fruits);
}

Leave a Comment