More about Collection:-

Let’s discuss some properties of the interfaces we have in collection:

Image for post
Image for post
Common methods

Above are the given common methods of collection, which every interface implements in collection.

Here, some properties which will help you to understand , which interface is best for your application, according to given requirements.

Image for post
Image for post

List:-

It is child interface of Collection.

If we ant to represent a group of individual objects as a single entity where duplicates are allowed and insertion order must be preserved(means order is not important), then we should go for list.

ArrayList:-

=Underlying data structure in arrayList is resize able array , as it is under List so duplicates are allowed and order is preserved.

=Heterogeneous objects(different type of) can be inserted .

=null insertion is also possible.And it implements RandomAccess interface which allows us to make retrieval operations easily

=Best choice Array List when our frequent operation is retrieval operation.

=worst choice when our frequent operation is insertion or deletion in middle.

LinkedList:-

=underlying data structure is doubly Linked List

=And it inherit all properties of Array List except RandomAccess interface.

=That’s why LinkedList is best choice when our frequent operation is insertion or deletion in the middle, worst choice when frequent operation is retrieval.

Vector:-

=underlying data structure is array .

=All properties of ArrayList

=In vector most of the methods are synchronized, that’s why is it thread safe.

=Best choice for retrieval .

Stack:-

=It is child interface of vector.

=specially designed for Last In First Out operation.

HashSet:-

=Underlying data structure is Hash Table

=duplicates are not allowed , If we are trying to insert duplicates, we won’t get any compilation error or runtime error , add() method simply returns false.

=Insertion order is not preserved , all objects inserted according to hash code.

=Heterogeneous objects are allowed.

=Null insertion is possible.

=No implementation of RandomAccess interface.

=Best choice for search operations.

LinkedHashSet:-

=underlying data Structure is Hash Table + Linked List .

=Best choice to develop cache based applications where duplicates are not allowed and insertion order must be preserved.

SortedSet:-

=It is child interface of set.

=If we want to represent a group of individual objects according to some sorting order, then we must go for SortedSet.

TreeSet:-

=underlying data structure is balanced tree.

=duplicates are not allowed.

=Insertion order is not preserved , but there should be some sorting order.

=Heterogeneous objects are not allowed, if we try to insert then we will get runtime exception saying ClassCastException.

=Null insertion is allowed , but only for once.

thanks for giving your valuable time.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store