Java Collections – Interface, List, Queue, Sets

Java Collections – Interface, List, Queue, Sets

What are Java collections? Java collections refer to a collection of individual objects that are represented as a single unit. You can perform all operations such as searching, sorting, insertion, manipulation, deletion, etc., on Java collections just like you do it on data.

In this Java collections blog, where we will understand each aspect of it in the following sequence:

  1. What is a Java Collection Framework?
  2. Why use Java Collection?
  3. Java Collection framework Hierarchy
  4. Interface 
  5. List
  6. Queue
  7. Sets

What is a Java Collection Framework?

A Java collection framework provides an architecture to store and manipulate a group of objects. A Java collection framework includes the following:

  • Interfaces
  • Classes
  • Algorithm

Let’s learn about them in detail:

Interfaces: Interface in Java refers to the abstract data types. They allow Java collections to be manipulated independently from the details of their representation. Also, they form a hierarchy in object-oriented programming languages.

Classes: Classes in Java are the implementation of the collection interface. It basically refers to the data structures that are used again and again.

Algorithm: Algorithm refers to the methods which are used to perform operations such as searching and sorting, on objects that implement collection interfaces. Algorithms are polymorphic in nature as the same method can be used to take many forms or you can say perform different implementations of the Java collection interface.

Why use Java Collection?

There are several benefits of using Java collections such as:

  • Reducing the effort required to write the code by providing useful data structures and algorithms.
  • Java collections provide high-performance and high-quality data structures and algorithms thereby increasing the speed and quality
  • Unrelated APIs can pass collection interfaces back and forth
  • Decreases extra effort required to learn, use, and design new API’s
  • Supports reusability of standard data structures and algorithms

Java collections framework Hierarchy

As we have learned Java collection framework includes interfaces and classes. Now, let us see the Java collections framework hierarchy.

No alt text provided for this image

Java Collection: Interface

Iterator interface : Iterator is an interface that iterates the elements. It is used to traverse the list and modify the elements. Iterator interface has three methods which are mentioned below: 

  1. public boolean hasNext() – This method returns true if the iterator has more elements.
  2. public object next() – It returns the element and moves the cursor pointer to the next element.
  3. public void remove() – This method removes the last elements returned by the iterator. 

There are three components that extend the collection interface i.e List, Queue and Sets. Let’s learn about them in detail:

Java Collection: List

A List is an ordered Collection of elements which may contain duplicates. It is an interface that extends the Collection interface. Lists are further classified into the following:

  1. ArrayList
  2. LinkedList
  3. Vectors

Array list: ArrayList is the implementation of List Interface where the elements can be dynamically added or removed from the list. Also, the size of the list is increased dynamically if the elements are added more than the initial size. 

Syntax: ArrayList object = new ArrayList ();

Some of the methods in array list are listed below: 

  1. boolean add(Collection c) -Appends the specified element to the end of a list. 
  2. void add(int index, Object element) -Inserts the specified element at the specified position. 
  3. void clear() -Removes all the elements from this list. 
  4. int lastIndexOf(Object o)- Return the index in this list of the last occurrence of the specified element, or -1 if the list does not contain this element. 
  5. Object clone() -Return a shallow copy of an ArrayList. 
  6.  Object[] toArray() -Returns an array containing all the elements in the list. 
  7. void trimToSize() -Trims the capacity of this ArrayList instance to be the list’s current size.

Linked List: Linked List is a sequence of links which contains items. Each link contains a connection to another link. 

Syntax: Linkedlist object = new Linkedlist();

Some of the methods in the linked list are listed below:

  1.  boolean add( Object o) -It is used to append the specified element to the end of the vector. 
  2. boolean contains(Object o) - Returns true if this list contains the specified element.  
  3. void add (int index, Object element) -Inserts the element at the specified element in the vector. 
  4. void addFirst(Object o) -It is used to insert the given element at the beginning. void addLast(Object o) -It is used to append the given element to the end. 
  5. int size() -It is used to return the number of elements in a list
  6. boolean remove(Object o) -Removes the first occurrence of the specified element from this list.
  7.  boolean remove(Object o) -Removes the first occurrence of the specified element from this list. 
  8. int indexOf(Object element)-Returns the index of the first occurrence of the specified element in this list, or -1. 
  9. int lastIndexOf(Object element)-Returns the index of the last occurrence of the specified element in this list, or -1.

Vectors : Vectors are similar to arrays, where the elements of the vector object can be accessed via an index into the vector. Vector implements a dynamic array. Also, the vector is not limited to a specific size, it can shrink or grow automatically whenever required. It is similar to ArrayList, but with two differences :

  • Vector is synchronized.
  • Vector contains many legacy methods that are not part of the collections framework.

Syntax:Vector object = new Vector(size,increment);

Below are some of the methods of the Vector class:

  1. boolean add(Object o) -Appends the specified element to the end of the list. 
  2. void clear() -Removes all of the elements from this list.  
  3. void add(int index, Object element) -Inserts the specified element at the specified position. 
  4. boolean remove(Object o) -Removes the first occurrence of the specified element from this list. 
  5. boolean contains(Object element) -Returns true if this list contains the specified element. 
  6. int indexOfObject (Object element) -Returns the index of the first occurrence of the specified element in the list, or -1. 
  7. int size() -Returns the number of elements in this list.int
  8. lastIndexOf(Object o) -Return the index of the last occurrence of the specified element in the list, or -1 if the list does not contain any element.

Java Collection: Queue

Queue in Java follows a FIFO approach i.e. it orders the elements in First In First Out manner. In a queue, the first element is removed first and last element is removed in the end. Each basic method exists in two forms: one throws an exception if the operation fails, the other returns a special value. Also, priority queue implements Queue interface. The elements of the priority queue are ordered according to their natural ordering, or by a Comparator provided at the queue construction time. The head of this queue is the least element with respect to the specified ordering.

Below are some of the methods of Java Queue interface:

  1. boolean add(object) -Inserts the specified element into the queue and returns true if it is a success. 
  2. boolean offer(object) -Inserts the specified element into this queue. 
  3. Object remove() -Retrieves and removes the head of the queue. 
  4. Object poll() -Retrieves and removes the head of the queue, or returns null if the queue is empty. 
  5. Object element() -Retrieves, but does not remove the head of the queue. 
  6. Object peek() -Retrieves, but does not remove the head of this queue, or returns null if the queue is empty.

Java Collection: Sets

A Set refers to a collection that cannot contain duplicate elements. It is mainly used to model the mathematical set abstraction. Set has its implementation in various classes such as HashSet, TreeSetand LinkedHashSet.

Let’s go into detail on each one of them:

HashSet: Java HashSet class creates a collection that use a hash table for storage. Hashset only contain unique elements and it inherits the AbstractSet class and implements Set interface. Also, it uses a mechanism hashing to store the elements. 

Below are some of the methods of Java HashSet class:

  1. boolean add(Object o) -Adds the specified element to this set if it is not already present. 
  2. boolean contains(Object o) -Returns true if the set contains the specified element. 
  3. void clear() -Removes all the elements from the set. 
  4. boolean isEmpty() -Returns true if the set contains no elements. 
  5. boolean remove(Object o) -Remove the specified element from the set. 
  6. Object clone() -Returns a shallow copy of the HashSet instance: the elements themselves are not cloned. 
  7. Iterator iterator() -Returns an iterator over the elements in this set. 
  8. int size() -Return the number of elements in the set.

Linked Hashset : Java LinkedHashSet class is a Hash table and Linked list implementation of the set interface. It contains only unique elements like HashSet. Linked HashSet also provides all optional set operations and maintains insertion order. Let us understand these linked Hashset with a programmatic example:

TreeSet : TreeSet class implements the Set interface that uses a tree for storage. The objects of this class are stored in the ascending order. Also, it inherits AbstractSet class and implements NavigableSet interface. It contains only unique elements like HashSet. In TreeSet class, access and retrieval time are faster.

Below are some of the methods of Java TreeSet class:

  1. boolean addAll(Collection c) -Add all the elements in the specified collection to this set. 
  2. boolean contains(Object o) -Returns true if the set contains the specified element. 
  3. boolean isEmpty() -Returns true if this set contains no elements. 
  4. boolean remove(Object o) -Remove the specified element from the set. 
  5. void add(Object o) -Add the specified element to the set. 
  6. void clear() -Removes all the elements from the set. 
  7. Object clone() -Return a shallow copy of this TreeSet instance. 
  8. Object first() -Return the first element currently in the sorted set. 
  9. Object last() -Return the last element currently in the sorted set. 
  10. int size() -Return the number of elements in the set.














To view or add a comment, sign in

Others also viewed

Explore content categories