top of page
hand-businesswoman-touching-hand-artificial-intelligence-meaning-technology-connection-go-

Basics of Java Collections for Beginners

Introduction

Collection: It is used to represent a group of individual objects as a single entity.

Collections in java: Collection is an interface which is available in java.util package.


Features of Collection

● Collection is growable in nature.

● Store both homogenous and heterogenous data elements.

● Every collection is based on standard data structure so remade method support is available.


Java Collections Framework

The Java collections framework is a set of classes and interfaces that implement commonly reusable collection data structure.


Collection(I)

● If we want to represent a group of individual objects as a single entity, then we should go for collection.

● Collection interface defines the most common method which is applicable for any type of collection.

● Collection interface is a root interface of all collections.

●There is no concrete class which implements collection interface.


a) List(I)

● If we want to represent a group of individual objects as a single entity where duplicates are allowed and insertion order must be preserve then we should go for List.

● List is a child interface of collection.


i) ArrayList

●The underlying data structure is a growable array.

●Duplicates are allowed.

●Insertion order is preserved.

●Heterogeneous objects are allowed.

●Null insertion is allowed.

●It implements Serializable, Cloneable and Random-access interface.

●It is the best choice when frequent operation is retrieval.

●It is the worst choice when frequent operation is insertion or deletion in middle.


ii) Vector

● Underlying data structure is growable array.

● Duplicates are allowed.

● Insertion order is preserved.

● Heterogeneous objects are allowed.

● Null insertion is allowed.


iii) LinkedList

● Duplicates are allowed.

● Insertion order is preserved.

● Heterogeneous objects are allowed.

● Null insertion is allowed.

● LinkedList implement Serializable, Cloneable but not Random-access interface.

● LinkedList is the best choice when frequent operation is insertion and deletion in middle.

● The worst choice when frequent operation is retrieval.


b) Set(I)

● If we want represent group of individual objects as single entity where duplicates are not allowed, and insertion order not preserve then we should go for Set.


i) SortedSet

● If we want to represent group of individual objects as single entity where duplicates are not allowed, and all objects are inserted according to some sorting order then we should go for SortedSet.


ii) NavigableSet

● Child interface of set interface.

● It defines several methods for navigation purpose.


iii) HashSet

● Underlying data structure is hash table

● Duplicates are not allowed if we trying to add duplicate, we won’t any error method simply return false

● Insertion order is not preserved. All objects will be inserted as hash code of object.

● Heterogeneous objects are allowed

● Null insertion is possible.

● Implements serializable and cloneable interface but not random-access interface.

● It is best choice when your frequent operation is search.


iv) TreeSet

● Underlying data structure binary tree

● Duplicates are not allowed if we trying to add duplicate, we won’t any error method simply return false

● Insertion order is not preserved. All objects will be inserted in sorting order.

● Heterogeneous objects are not allowed

● Null insertion is possible.

● Sort and navigation


c) Queue

● It is child interface of collection interface.

● If we want to represent group of individual objects prior to processing then we should go with queue.


Map

● Map is not a child interface of collection.

● If we want to represent a group of individual objects as key value pair, then we should go for map interface.

● Both key and value are objects

● Duplicate key is not allowed but value can be duplicated.


a) SortedMap

● It is a child interface of map.

● If we want to represent a group of key value pairs according to some sorting order of key, then we should go for SortedMap.


b) NavigableMap

● It is a child interface of SortedMap.

● It defines several utility methods for navigation purposes.

● It defines several utility methods for navigation purposes.


Differences

a) Differences between List & Set

b) Differences between Array & Collection



Happy Learning!!!

Thank You!

82 views0 comments

Recent Posts

See All

Exception Handling in Selenium Webdriver

What is an exception? An exception is an error that occurs during the execution of a program. However, while running a program, programming languages generate an exception that must be handled to prev

Comments

Rated 0 out of 5 stars.
No ratings yet

Add a rating
bottom of page