Introduction to Collection Framework

Collection is a group of objects represented by single entity like array of integers,array of string etc. If there’s an array to represent group of objects then why need collection framework for the same.

As we know that array is static in size means we can’t grow array size at run time. So there is wastage/lack of memory due to this.

Why to learn Collection Framework?

This is big question asked by interviewers from java programmers. They actually want to know your concept on this.

We know that collection of elements is always required for any kind of projects that’s Collection Framework importance is more.If you don’t use collection framework then you need to use array to store collection of information. As you know that array is static in size and array doesn’t have any underlying data structure and no utility methods to work on those store elements.

Following reason behind to use Collection Framework

  1. Array is static data structure means size of array need to be defined before use it and can’t be increased at run-time.
  2. Array doesn’t provide utility methods to perform any operations on stored elements.
  3. Array doesn’t have any underlying data structure where as collection framework have several implemented classes based on different data structure algorithm.
  4. Collection Framework provides a set of interfaces and classes which can be used as per need of your project.

Collection Framework is an API which consists a set of interfaces and classes. Before collection framework we have Dictionary, Vector, Stack and Properties to store and manipulate groups of objects. But these were not have any underlying data structure that’s these are too restructured in Java 2 and also introduced collection framework api.

java.util package contains the classes and interfaces for collection framework. Collection Framework defines the several data structures API using interfaces. One can implement them if there’s need for it. The implemented classes also provided by them as listed below.

Interfaces

  1. Iterable
  2. Collection
  3. List
  4. Set
  5. Sorted Set
  6. Queue
  7. Dequeue

Other Interfaces

  1. Map
  2. Sorted Map

Implementation Classes

  1. List – ArrayList,LinkedList,Vector,Stack
  2. Set – HashSet,LinkedHashSet
  3. Sorted Set – TreeSet
  4. Queque – PriorityQueque
  5. Deque – ArrayDeque

Which Collection class should choose?

This is really important point before you working on architecture of project. Because if you chose wrong data structure may lead low performance.

  1. If you need fast traversal ArrayList should be used.
  2. If you need frequent addition/deletion LinkedList can be used.
  3. If you need unique elements to be stored with no sort order then HashSet can be used. It works on hashing principle.
  4. If you need unique elements to be stored with insertion order to be maintain LinkedHashSet. Means elements will be shown as inserted. Null elements allowed in this collection.
  5. If you need unique elements to be stored with quick access and sorting order then choose TreeSet. It is faster than both HashSet and LinkedHashSet.
  6. If you want to store entry with key value pair without order to be maintained HashMap.
  7. If you need to store key value pair with insertion order to be maintained then choose LinkedHashMap.
  8. If you need to store key value pair with sorting order to be maintained then choose TreeMap.
  9. If you want FIFO(First In First Out) access mechanism then chosen and also can set priority of each element PriorityQueque. Like process some tasks where you need to process them as they received. You can have some task priority too while receiving.
  10. If you need insertion and deletion at both end then you can choose ArrayDeque which double ended queue. It is more faster than LinkedList and Stack.
  11. If you need LIFO(Last In First Out) order to be maintained then choose Stack.

Leave a Reply