Java Collection Framework-1
Java Collection Framework-1
• Interfaces
• Classes
• Algorithm
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.
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.
The Java collection framework provides the developers to access prepackaged data
structures as well as algorithms to manipulate data. Next, let us move to the
Java collections framework hierarchy and see where these interfaces and
classes resides.
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 collections: 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:
Method Description
boolean add(Collection
Appends the specified element to the end of a list.
c)
Object[] toArray() Returns an array containing all the elements in the list.
import java.util.*;
class ArrayListExample{
al.add("Tyler");
Iterator itr=al.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
}
In the above code, it will return the names that we have added using add() method i.e:
Jack
Tyler
Linked List: Linked List is a sequence of links which contains items. Each link contains
a connection to another link.
Java Linked List class uses two types of Linked list to store the elements:
Singly Linked List: In a singly Linked list each node in this list stores the data of the
node and a pointer or reference to the next node in the list. Refer to the below image to
get a better understanding of single Linked list.
Doubly Linked List: In a doubly Linked list, it has two references, one to the next
node and another to previous node. You can refer to the below image to get a better
understanding of doubly linked list.
import java.util.*;
al.add("Rahul");
al.add("Rajat");
while(itr.hasNext()){
System.out.println(itr.next());
}
}
Rachit
Rahul
Rajat
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:
Method Description
Appends the specified element to the end of the
boolean add(Object o)
list.
void clear() Removes all of the elements from this list.
void add(int index, Object Inserts the specified element at the specified
element) position.
Removes the first occurrence of the specified
boolean remove(Object o)
element from this list.
boolean contains(Object Returns true if this list contains the specified
element) element.
int Returns the index of the first occurrence of the
indexOfObject (Object element) specified element in the list, or -1.
int size() Returns the number of elements in this list.
Return the index of the last occurrence of the
int lastIndexOf(Object o) specified element in the list, or -1 if the list does
not contain any element.
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.
Method Description
boolean Inserts the specified element into the queue and returns true if it
add(object) is a success.
boolean
Inserts the specified element into this queue.
offer(object)
Object element() Retrieves, but does not remove the head of the queue.
Retrieves, but does not remove the head of this queue, or returns
Object peek()
null if the queue is empty.
class QueueExample {
queue.add("Amit");
// adding elements
queue.add("Rachit");
queue.add("Rahul");
System.out.println("head:"+queue.element());
System.out.println("head:"+queue.peek());
Iterator itr=queue.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
queue.remove();
queue.poll();
Iterator<String> itr2=queue.iterator();
while(itr2.hasNext()){
System.out.println(itr2.next());
}
In the above code, the output would be :
head:Amit
head:Amit
Amit
Rachit
Rahul
Rahul
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, TreeSet and LinkedHashSet.
HashSet: Java HashSet class creates a collection that use a hash table for
storage. HashSet only contain unique elements and it inherits the abstract Set 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:
Method Description
boolean add(Object o) Adds the specified element to this set if it is not already present.
boolean contains(Object
Returns true if the set contains the specified element.
o)
import java.util.*;
class HashsetExample{
al.add("Amit");
al.add("jack");
Iterator itr=al.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
The output of the above code would be:
Amit
Rachit
jack
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.
import java.util.*;
class LinkedHashsetExample{
al.add("Rick");
al.add("Sam");
Iterator itr=al.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
Mariana
Rick
Sam
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:
Method Description
boolean addAll(Collection c) Add all the elements in the specified collection to this set.
boolean contains(Object o) Returns true if the set contains the specified element.
Object first() Return the first element currently in the sorted set.
Object last() Return the last element currently in the sorted set.
import java.util.*;
class TreeSetExample{
al.add("Sam");
al.add("Rick");
Iterator itr=al.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
John
Rick
Sam