Collections in java

In this tutorial, we will see  about Collections in java.

Collection framework is core part of java programming language. It is used in almost all the applications. Collections framework provides a way to store and retrieve the collection of elements. This framework has many interfaces and classes which makes programmer life easy.

Let’s go through interfaces and classes of Collections in java.

Interfaces in Collection framework

Collection

Collection interface is root interface in collection hierarchy. A collection represents a group of objects, known as its elements. Java does not provide any direct implementation of this interface.

Collection interface provides methods to add or remove the elements. It also provides methods to check the size of the interface such as size, isEmpty. It also provides methods to check if element is part of the collection by using contains method.

List

List interface represents ordered collection which can contain duplicate elements. It extends collection interface.Classes which implement this interface have full control over where in the list each element is inserted. The user can access elements by its index.

It is more like a dynamic array. ArrayList and LinkedList are implementation classes of List interface.

List interface provides methods to add to the specified index or remove an element from the specified index.

Set

Set interface represent a collection that can not contain duplicate elements.This interface models the mathematical set abstraction.Set contains no elements such that e1.equals(e2).

Hashset, LinkedHashSet and TreeSet are the implementations of Set interface.

Queue

Queue interface is designed for holding before processing elements.queues provide additional insertion, extraction, and inspection operations apart from basic Collection methods.

Queue interface provides two methods:one throws an exception if the operation fails, the other returns a special value (either null or false, depending on the operation)

Queues generally, but do not necessarily, order elements in a FIFO (first-in-first-out) manner. Among the exceptions are priority queues, which order elements according to a supplied comparator or the elements’ natural ordering, and LIFO queues (or stacks) which order the elements LIFO (last-in-first-out).

Deque

Deque stands for double ended queue and allows insertion and removal from both the end.This interface supports capacity restricted deques as well apart from no fixed limit queues.

This interface defines methods to access or remove the elements at both ends of the deque. Methods are provided to insert, remove, and examine the element.

Map

Map interface is used to store key value pairs.It does not allow duplicate keys. Implementations of Map interface are HashMap,LinkedHashMap TreeMap

SortedSet

SortedSet is used to define ordering of element on Set.Elements of Collection can be sorted by natural ordering or comparator provided by user of this interface.
This interface provides important methods based on ordering such as first, last, headSet and tailSet.

SortedMap

SortedMap is used to define the ordering of elements on keys of Map.Elements of map can be sorted by natural ordering or comparator provided by the user of this interface.
This interface provides important methods based on ordering such as firstKey, lastKey, headMap and tailMap.

NavigableMap

NavigableMap extends from SortedMap and has methods which can help in returning the closest matches for given search targets
It provides methods
lowerEntry: Entry objects with key less than a given key.
floorEntry: Entry objects with key less than or equal to a given key.
ceilingEntry: Entry objects with key greater than or equal to a given key.
higherEntry: Entry objects with key greater than a given key.

TreeMap is the implementation of this interface.

NavigableSet

NavigableSet extends from SortedSet and has methods which can help in returning the closest matches for given search targets
It provides methods.
lower: Elements less than a given key.
floor: Elements less than or equal to a given key.
ceiling: Elements greater than or equal to a given key.
higher:  Elements greater than a given key.

TreeSet is the implementation of this interface.

Classes in Collection framework

ArrayList

ArrayList is one of the most used class in java programs.

Here are some links which will help you to learn ArrayList

HashMap

HashMap stores key-value pairs.

Here are some links which will help you to learn HashMap

LinkedHashMap

LinkedHashMap also stores key-value pairs but maintains order as well.

Here is a link to learn about LinkedHashMap.

TreeMap

TreeMap also stores key-value pairs but stores key either in natural sorted order(Comparable) or using comparator.

Here is the link to learn about TreeMap.

HashSet

HashSet implements Set interface and does not allow d uplicates.It internally uses HashMap to avoid duplicates.

Here is the link to learn about HashSet.

LinkedHashSet

LinkedHashSet is similar to HashSet but it also maintains insertion order.

Here is the link to learn about LinkedHashSet.

TreeSet

TreeSet sorts the elements either by natural order or using provided Comparator.

Here is the link to learn about TreeSet.

That’s all about Collections in java.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *