Collections Framework in java

The java collections framework is a collection of interfaces and classes which help the programmer to perform operations on data like sorting, searching ,deleting , manipulating etc. In simple words, it helps in string and processing the data efficiently.

About this framework:

  • Rather than writing your own collection classes, Java provides these ready-to-use collection classes.
  • It is ready-made architecture which makes your life easy to play around data.
  • Collection works a bit like an array , but is more flexible which allows to change its size dynamically in more efficient way and have more advance features to iterate over the data.
  • Java collections are located in the java.util package.
  • Having a unified architecture.
  • It is optional.

Hierarchy of Collection Framework:

All the interfaces and classes are located inside java.util package. Let’s see the hierarchy of collections framework below.

Collections Framework


Map Interface

We will discuss them in detail with examples and code. Let’s understand them one by one. You will find links for all these explanation on this page. Click on those links to read about them in more detail.

List:

List is an ordered collection of objects, that means you can access the objects in a specific order. Elements can be inserted and accessed by their position in the list and it start with zero index. List may contain duplicate elements that means you can insert the same element many times.

Set:

A Set is a collection that can not contain duplicate elements. Two set instance will be considered same if having same elements. It has three general-purpose Set implementations as : HashSet, TreeSet and LinkedHashSet. HashSet –  stores elements in a hashtable and is best-performing implementation, however it makes no guarantees concerning the order of iteration.
TreeSet - stores its elements in a red-black tree, orders its elements based on their values; it is substantially slower than HashSet.
LinkedHashSet - which is implemented as a hash table with a linked list running through it, orders its elements based on the order in which they were inserted into the set (insertion-order).

Map:

Map is an object that implements combination of key and value pair. A value will be mapped to a key and can be accessed by that key. A map cannot contain duplicate keys: Each key can map to at most one value.

There are mainly implementations of Map interfaces: HashMap, TreeMap, and LinkedHashMap.
HashMap – It makes no guarantees concerning the order of iteration.
TreeMap – It stores its elements in a red-black tree, orders its elements based on their values; it is substantially slower than HashMap.
LinkedHashMap – It orders its elements based on the order in which they were inserted into the set (insertion-order).

Iterator/ListIterator

Iterator and ListIterator both are used to iterate over the collection of elements. The main difference is that Iterator can traverse in forward direction only and ListIterator is capable of traversing in both forward and backward direction.

You can find more difference between iterator and ListIterator here.

This is box title
Have any question or suggestion for us?Please feel free to post in Q&A Forum