In this tutorial, we will learn about what is the Collections, types of collections in Java and walkthrough on list of classes available based on its type.
Java Collections is a framework which provides set of interfaces and classes to provide, main goal of the collections is to group several objects and perform operations over a grouped object. Grouping of objects means storing of objects in to the memory of JVM,
While storing objects, collections follow an organized way of choosing an appropriate data structure. Each class of the collection follows a single data structure it can be either linear or non-linear data structure.
Collections are broadly classified as follows.
- List based collections
- Set based collections
- Queue based collections
List based collections
List based collections allows to store elements(Objects) in a sequential order of index and iterating elements also done in order which they added, the following are the features of List collections.
- Linear data structures are used.
- Dynamic size in nature(N numbers of elements can be added to form a group).
- Allows random access of elements based on index of element.
- Allows duplicate elements.
- Size of the data structure is not fixed.
- All classes under list implements java.util.List interface.
Based on the type of the linear data structure and its behaviour, list collections are further divided into as follows.
It uses Array based data structure, which will have initial capacity. Capacity will grow when number of elements reached to its initial capacity by creating a new array with larger size and copies the existing elements from old array to new array .
As the name implies, it uses Linked List data structure and contains initial capacity. When number of elements reached to its initial capacity will not copy the elements to other data structure because internally it uses node based linkage.
It uses Array based data structure like ArrayList but all the operations of Vector are thread safe. Only one operation can be performed at a time performance of the Vector is poor as compared to the ArrayList.
It extends Vector class and provides an additional functionality as last in first out. Additional methods of stack as compared to vector are push, pop and peek.
Set based collections
As the name implies set based collections will not allow duplicate elements, only one null element can be added and the following are the feature of set collections.
- Uses non-linear data structure.
- Random access is not possible.
- Uses hash code and equals method of element class to identify duplicates.
- Only one null element can be inserted
- Dynamic size in nature.
- All classes under set implements java.util.Set interface
Only two classes are available for set based collections.
Only difference from HashSet to LinkedHashSet is insertion order maintenance, where LinkedHashSet maintains it and HashSet not maintains insertion order.
In LinkedHashSet next iterating element can be predicted but in case HashSet not as insertion order is not maintained by it.
Queue based collections
As the name implies, it follows the order of first in and first out(FIFO). It uses the linear data structure and the following features of the Queue.
- Follows the order of insertion.
- Removal takes place rear end whereas adding takes in the front end.
- Exit will happen in the order of first in and first out.
- Deletion takes place only at the front end.
The following is only one class implements it and one interface extends it from the collections.
- PriorityQueue is a class implements Queue
- Deque is an interface which extends Queue
In this tutorial, we have covered what is collections and high-level understanding on classes and interfaces which are part of collections.