As told in the title, this is an overview of java collections framework. The word “Collections” literarily means a group of things and a collection of interfaces and classes constitute Java Collections Framework.
Introduction to Collections Framework Overview
As mentioned earlier, collections contain a group of objects and unlike in the case of arrays, all collections can dynamically shrink or grow in size. A collection can hold objects in three different methods
- Set: – A collection of unique objects. A set cannot hold two equal objects.
- List: – A sequence of objects and it can carry duplicates or equal objects and it keeps the objects in the order they are inserted.
- Queue: – A Queue has two sides. New elements will be added in the tail end while it will be removed from the head.
There are many interfaces in the collections framework. Each method (set, list, and Queue) has their own interfaces. Now let us see what are the interfaces in collection framework.
There are five different interfaces for the set and they are HashSet, LinkedHashSet, TreeSet, SortedSet and Navigable Set.
HashSet, Linked HashSet and TreeSet
They are the for implementation of set and HashSet is the implementation by default which is used mostly. LinkedHashSet can be called as a combination of HashSet with List as it will not allow duplicate contents as the set but traverses its elements in the same order as they are inserted. At the same time, the TreeSet always maintains a sorted order always.
SortedSet and Navigable Set
These two are subinterfaces. As the name indicates, SortedSet is always sorted and the Navigable Set which has been added to Java 6 helps to navigate through sorted list.
List has only two interfaces and they are ArrayList and LinkedList
ArrayList and LinkedList
ArrayList is the implementation by default for List. It allows duplicate objects and iteration in the order of insertion. It is very fast to iterate and to read from as it is based on Arrays but it is very slow while adding or removing or elements at random positions. At the same time, LinkedList is slow to iterate and read from but quite faster for adding or removing elements at any position.
LinkedList, implements Queue also while implementing the list as its doubly linked list makes it easy to implement Queue. Besides LinkedList Queue has Priority Queue also as another Queue implementation. Here the objects will be get ordered automatically. Its function is almost same as that of TreeSet except that it allows duplicate objects or entries.
These are the basics of Java Collections Framework and more details can be learned in Java tutorials.