[Go to site: main page, start]

0% found this document useful (0 votes)
14 views6 pages

Java Collection Framework Overview

Uploaded by

deyavilash123
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views6 pages

Java Collection Framework Overview

Uploaded by

deyavilash123
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java Collection Framework

The Collection in Java is a framework that provides architecture to store and


manipulate the group of objects.

Java Collections can achieve all the operations that you perform on a data such as
searching, sorting, insertion, manipulation, and deletion.

Java Collection means a single unit of objects. Java Collection framework provides
many interfaces (Set, List, Queue, Deque) and classes (ArrayList, Vector,
LinkedList, PriorityQueue, HashSet, LinkedHashSet, TreeSet).

What is Collection framework

The Collection framework represents a unified architecture for storing and


manipulating a group of objects. It has:

Interfaces and its implementations, i.e., classes

Algorithm

Hierarchy of Collection Framework

Let us see the hierarchy of Collection framework. The [Link] package contains
all the classes and interfaces for the Collection framework.
Iterator interface

Iterator interface provides the facility of iterating the elements in a forward direction only.

Methods of Iterator interface

There are only three methods in the Iterator interface. They are:

No. Method Description


public boolean It returns true if the iterator has more elements otherwise it returns
1
hasNext() false.
It returns the element and moves the cursor pointer to the next
2 public Object next()
element.
3 public void remove() It removes the last elements returned by the iterator. It is less used.

Iterable Interface
The Iterable interface is the root interface for all the collection classes. The Collection interface
extends the Iterable interface and therefore all the subclasses of Collection interface also
implement the Iterable interface.

It contains only one abstract method. i.e.,

1. Iterator<T> iterator()

It returns the iterator over the elements of type T.

Collection Interface
The Collection interface is the interface which is implemented by all the classes in the collection
framework. It declares the methods that every collection will have. In other words, we can say
that the Collection interface builds the foundation on which the collection framework depends.

Some of the methods of Collection interface are Boolean add ( Object obj), Boolean addAll ( Collection
c), void clear(), etc. which are implemented by all the subclasses of Collection interface.

List Interface
List interface is the child interface of Collection interface. It inhibits a list type data structure in
which we can store the ordered collection of objects. It can have duplicate values.

List interface is implemented by the classes ArrayList, LinkedList, Vector, and Stack.

To instantiate the List interface, we must use :

1. List <data-type> list1= new ArrayList();


2. List <data-type> list2 = new LinkedList();
3. List <data-type> list3 = new Vector();
4. List <data-type> list4 = new Stack();

ArrayList

The ArrayList class implements the List interface. It uses a dynamic array to store the duplicate
element of different data types. The ArrayList class maintains the insertion order and is non-
synchronized. The elements stored in the ArrayList class can be randomly accessed. Consider the
following example.
import [Link].*;

class TestJavaCollection1{

public static void main(String args[]){

ArrayList<String> list=new ArrayList<String>();//Creating arraylist

[Link]("Ram");//Adding object in arraylist

[Link]("Ajay");

[Link]("Ravi");

[Link]("Ajay");

//Traversing list through Iterator

Iterator itr=[Link]();

while([Link]()){

[Link]([Link]());

Output:

Ram
Ajay
Ravi
Ajay

LinkedList

LinkedList implements the Collection interface. It uses a doubly linked list internally to store the
elements. It can store the duplicate elements. It maintains the insertion order and is not
synchronized. In LinkedList, the manipulation is fast because no shifting is required.

Consider the following example.

import [Link].*;

public class TestJavaCollection2{


public static void main(String args[]){

LinkedList<String> al=new LinkedList<String>();

[Link]("Ravi");

[Link]("Vijay");

[Link]("Ravi");

[Link]("Ajay");

Iterator<String> itr=[Link]();

while([Link]()){

[Link]([Link]());

Output:

Ravi
Vijay
Ravi
Ajay

Set Interface
Set Interface in Java is present in [Link] package. It extends the Collection interface. It represents the
unordered set of elements which doesn't allow us to store the duplicate items. We can store at most one
null value in Set. Set is implemented by HashSet, LinkedHashSet, and TreeSet.

Set can be instantiated as:

Set<data-type> s1 = new HashSet<data-type>();

Set<data-type> s2 = new LinkedHashSet<data-type>();

Set<data-type> s3 = new TreeSet<data-type>();


HashSet

HashSet class implements Set Interface. It represents the collection that uses a hash table for storage.
Hashing is used to store the elements in the HashSet. It contains unique items.

Consider the following example.

import [Link].*;

public class TestJavaCollection7{

public static void main(String args[]){

//Creating HashSet and adding elements

HashSet<String> set=new HashSet<String>();

[Link]("Ravi");

[Link]("Vijay");

[Link]("Ravi");

[Link]("Ajay");

//Traversing elements

Iterator<String> itr=[Link]();

while([Link]()){

[Link]([Link]());

Output:

Vijay

Ravi

Ajay

Common questions

Powered by AI

The traversal mechanisms in Java’s Collection Framework differ based on the structure and intended use of each collection type. In List implementations like ArrayList and LinkedList, elements can be traversed in both forward and backward directions using ListIterator, in addition to standard forward traversal with Iterator. Sets, being unordered by definition, are typically traversed using Iterator, albeit without any guaranteed order (except LinkedHashSet which maintains insertion order). Queue implementations are designed for processing elements in a FIFO (First-In-First-Out) manner, allowing traversal via Iterator, but typically accessed using methods like poll() or remove() to respect their processing order .

ArrayList uses a dynamic array to store its elements, allowing for fast random access due to its backing data structure. However, this also means that insertions and deletions are time-consuming as elements need to be shifted when an element is added or removed from any position other than the end. LinkedList, on the other hand, uses a doubly linked list as its internal data structure, allowing for efficient insertions and deletions from both ends of the list without the need for element shifting. This makes LinkedList preferable when frequent modifications are required, particularly at the beginning or the end of the list .

Both ArrayList and LinkedList are not synchronized, making them unsuitable for concurrent access by multiple threads without external synchronization. In multi-threaded environments, synchronization must be manually applied to ensure thread safety. This can be achieved, for example, by using synchronized blocks or by wrapping the collection using Collections.synchronizedList() or similar utilities provided by the Collections class. Lack of synchronization can lead to ConcurrentModificationException, among other issues, if structure-modifying operations are performed concurrently .

The List interface in Java Collection Framework allows storing an ordered collection of objects where duplicate values are permitted. It maintains the insertion order of the elements, meaning elements can be accessed in the same sequence they were added. On the other hand, the Set interface represents a collection that does not allow duplicate items and is unordered; however, some implementations like LinkedHashSet maintain insertion order while still preventing duplicates .

The choice of data structure significantly affects performance in ArrayList and LinkedList. In ArrayList, which uses a dynamic array, element access by index is fast (O(1)), but inserting elements at the head (or any arbitrary position other than the end) requires shifting elements, resulting in O(n) complexity. Conversely, LinkedList, utilizing a doubly linked list, has O(n) time complexity for access as it may need to traverse from the head to the desired node. However, it allows for O(1) complexity insertion or deletion at the head (or tail), making it more suitable for frequent addition/removal operations, especially at the extremities .

The Iterable interface is considered the root interface of Java's collection framework because it is the baseline interface implemented by all collection classes. It defines the iterable nature of collections, requiring them to provide an iterator() method that returns an Iterator over elements of type T. Because the Collection interface extends Iterable, all its subclasses, including List, Set, and Queue, inherit the contract of being iterable. This relationship ensures that any collection can be traversed in a standardized way using an Iterator, which is central to handling collections generically .

The Iterator interface in Java's Collection Framework facilitates element iteration in a forward-only manner across a collection. Its main methods include hasNext(), next(), and remove(). hasNext() checks if there are more elements to iterate over, next() returns the next element and advances the iterator, and remove() removes the last element returned by the iterator, although it is less commonly used. These methods provide a standard way to traverse elements in a collection .

HashSet ensures the uniqueness of elements by utilizing a hash table where the hash code of elements is used to determine their storage location. When an element is added, its hash code is calculated, and the hash table checks if this location is already occupied. If an element with the same hash code already exists, the new element is not added, hence preventing duplicates. This hashing mechanism ensures that all elements are unique within the HashSet .

In Java's Collection Framework, the hierarchy of interfaces supports polymorphism by defining a set of operations that can be performed on collections while leaving the specific details of implementation to concrete classes. The Collection interface forms the root of this hierarchy and is extended by more specific interfaces such as List, Set, and Queue, each with its own specialized contracts. Classes like ArrayList, HashSet, and LinkedList implement these interfaces, providing actual functionally according to the requirements of the interface they implement. This polymorphic structure allows a single method to operate on instances of multiple classes at different levels in this hierarchy, thus achieving flexibility and ease of maintenance in the code .

The remove() method in the Iterator interface, while infrequently employed, serves a crucial role in modifying a collection safely while iterating over its elements. This method allows for the removal of the last element returned by the iterator, effectively preventing ConcurrentModificationException which can occur if a collection is structurally modified while being iterated by conventional means. While its use is less common because of the need for careful iterator manipulation, it provides a means for precise and controlled element removal in a loop without needing to restart the iteration process .

You might also like