Java Collection Framework

Sanduni Jayaweera
4 min readJun 8, 2021

The collection framework consists with the collection of objects. These objects allow to perform searching, sorting, insertion, deletion and manipulation. The collection framework consumes classes and interfaces itself.

Classes: Array List, Linked List, Vector, Priority Queue and etc.

Interfaces: Set, List, Que, Deque

By using the java collection framework, we can have following benefits.

The framework itself consumes pre-written objects, algorithms as well data structures. By using them, programmers can consider about the high-level portions of production while collection do the low-level integrations and works. Therefore, collection framework increases speed and quality of program while reducing programmer’s effort. As well, it provides interoperability between other APIs even they are not written in Java. The available interfaces and classes in collection are reusable.

Below figure clearly shows about the structure of classes and interfaces in java collection framework.

The green color boxes represent interfaces while others represent class. Also, dashed arrow implements its above interface or classes while straight arrows extends them.

Iterable Interface

This is root for all the interfaces and classes. Collection interface extends iterable while all other sub classes implement iterable.

Collection Interface

The collection interface provides a base for all other classes and interfaces in collection framework except iterable.

List Interface

List interface in Java used to store the list items in forward or backward directions and do operations like insert, delete, sort, search and update.

As well, the Array List, Linked List and Vector classes are directly implements the List interface. List interface can be found in java.util package.

Array List

At any time, we can add or remove elements from the Java array list. However by default, Array Lists are designed to hold 10 elements. But we can add or remove elements as our preferences. Therefore, Array List is more flexible than the using arrays in Java.

Differences between basic Array and Array List

  • Array has fixed size length while the size of Array List is dynamic.
  • Array used to represent via [] while Array List represent via different methods.
  • Array is faster than using the Array List since it has fixed size length.
  • In Arrays we use for loop for iteration purpose while Array List has a method called iterator() to iterate elements.

Linked List

The Lined List is a linear data structure where store the elements not in contiguous locations. Also Linked List is the implementation of doubly linked list. Which means a node (that has element in Linked List) has a pointer to its previous and next note in the list.

We can perform the operations like add (add element), remove (delete element by name or index), set (update or replace element) and so on.

Differences Between Array List and Linked List

  • Array List is dynamic array while Linked List is a doubly linked list.
  • Array List is slower than the Linked List since it need to shift the elements when we try to add or remove the elements.
  • Array is used for storing or accessing while Linked List useful for manipulating data.

Vector

Vector class consumes also dynamic array but synchronized. It is better to use vectors in multi threading environment. The elements in Vector can be accessed using the integer indexes. Also, Vector has its own methods than Array List which makes a different between them.

Stack

Stack class provides also a linear data structure which based on Last-In-First-Out (LIFO) order. Stack provides variety of methods like push, pop, search peek and so on.

Queue

Differ from Java Stack. Queue interface in Java Collections is based on the First-In-First-Out (FIFO) method.

This also consumes the method like boolean add(), boolean offer(), boolean remove(), boolean peek() and so on.

Dequeue

Dequeue stands for Doubly-Ended Queue. Dequeue can be used with the fixed size as well non-fixed size arrays.

Array Dequeue

The elements in Array Dequeue class can be removed from both sides. Also, there should not be null elements in here. Much more faster than Stack and Linked List.

Priority Queue

Priority queue interface is used when the objects are processing by its priority. Also, it does not hold the null values.

Set

It holds the unordered object and can not have the duplicated values as well. Basically Set Interface is used to store the mathematical values of Sets. It performs operations like intersection, union, difference and so on.

Hashed Set

This is one of the classes in Set interface which used to store the hash table in Java by using hashing. Hashed Set does not maintain an order and elements are stored by based on its hash code.

Linked Hash Set

This is a hash set with doubly-linked list.

  • The default constructor: LinkedHashSet()
  • Constructor with the element of collection: LinkedHashSet(Collection c)
  • Constructor with the size of linked hash set: LinkedHashSet(int size)
  • Constructor with capacity+load capacity: LinkedHashSet(int size, float load_capacity)

Sorted Set

Also used to store the mathematical sets but in a sorted manner.

Tree Set

Tree Set is the implementation of sorted set. And also it is non-synchronized.

  • The default constructor: TreeSet()
  • Constructor with the element of collection: TreeSet (Collection<? extends E >c)
  • Constructor with empty set which is sorted according to the constructor in future: (Comparator<? super E >Comparator)
  • Constructor with the sorted elements: TreeSet(SortedSet<E> s)

I hope you can get the clear basic understand about all of the interfaces and classes in Java Collection Framework.

Stay Safe !!!

--

--