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
- Array is static data structure means size of array need to be defined before use it and can’t be increased at run-time.
- Array doesn’t provide utility methods to perform any operations on stored elements.
- Array doesn’t have any underlying data structure where as collection framework have several implemented classes based on different data structure algorithm.
- 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.
- Sorted Set
- Sorted Map
- List – ArrayList,LinkedList,Vector,Stack
- Set – HashSet,LinkedHashSet
- Sorted Set – TreeSet
- Queque – PriorityQueque
- 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.
- If you need fast traversal ArrayList should be used.
- If you need frequent addition/deletion LinkedList can be used.
- If you need unique elements to be stored with no sort order then HashSet can be used. It works on hashing principle.
- 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.
- 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.
- If you want to store entry with key value pair without order to be maintained HashMap.
- If you need to store key value pair with insertion order to be maintained then choose LinkedHashMap.
- If you need to store key value pair with sorting order to be maintained then choose TreeMap.
- 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.
- 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.
- If you need LIFO(Last In First Out) order to be maintained then choose Stack.