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);
}

1) Collection(I): If we want to represent a group of individual objects as a single entity then we should go for collection.

  • The collection interface defines the most common methods which are applicable for any collection object.
  • In the general collection, the interface is considered as the root interface of the collection framework.
  • There is no concrete class that implements collection interface directly

Difference between collection and collections

  • The collection is an interface. If we want to represent a group of the individual object as a single entity then we should go for the collection
  • Collections is a utility class present in java. Util package to define several utility methods for collection object(like sorting, searching, etc)

2) List(I): It is the child interface of collection. If we want to represent a group of individual objects as a single entity where duplicates are allowed and insertion order must be preserved then we should go for List.

List(I) - Framework collection

You can learn this in detail in our institute which provides advanced Java training in Delhi and Core java training in Delhi.

Note:- in 1.2version vector and stack classes are re-engineered to implement List Interface.

3) Set(I): It is the child interface of collection. If we want to represent a group of individual objects as a single entity where duplicates are not allowed and insertion order not required then we should go for set Interface

Set(I) - Framework collection

4) SortedSet(I): It is the child interface of the set. If we want to represent a group of individual objects as a single entity where duplicates are not allowed and all objects should be inserted according to some sorting order, then we should go for a sorted set.

5) NavigableSet(I): It is the child interface of the sorted set. It contains several methods for navigation purposes.

NavigableSet(I)- Framework collection

6) Queue(Interface): it is the child interface of collection. If we want to represent a group of individual objects prior to processing then we should go for the queue. Usually, the queue follows first in first out the order but based on our requirement we can implement our own priority order also.

Queue (Interface)- Framework collection

7) Map(Interface): Map is not the child interface of Collection.

  • If we want to represent a group of objects as key-value pairs then we should go for a map.
  • Both keys and values are objects only duplicate keys are not allowed but values can be duplicated.
Map (Interface) - Framework collection

8) SortedMap(Interface): It is a child interface of a map.

  • If we want to represent a group of key-value pairs according to some sorting order of keys Then we should go for a sortedmap.
  • In a sortedmap, the sorting should be based on key but not based on value.

9) NavigableMap(Interface): It is the child Interface of sorted maps it defines several methods for navigation purposes.

NavigableMap (Interface) - Framework collection

Leave a Comment