0% found this document useful (0 votes)
3 views

java unit 4

The document provides an overview of the Java Collections Framework, detailing its features, advantages, and the various interfaces such as List, Queue, Set, and Map. It explains the characteristics and implementations of different data structures like ArrayList, LinkedList, HashSet, and TreeSet. Additionally, it highlights the differences between collections and the Collections utility class, emphasizing the importance of data structures in efficient data manipulation.

Uploaded by

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

java unit 4

The document provides an overview of the Java Collections Framework, detailing its features, advantages, and the various interfaces such as List, Queue, Set, and Map. It explains the characteristics and implementations of different data structures like ArrayList, LinkedList, HashSet, and TreeSet. Additionally, it highlights the differences between collections and the Collections utility class, emphasizing the importance of data structures in efficient data manipulation.

Uploaded by

andhadhun0786
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

UNIT -4 Collection Framework

##Collections in Java - In Java, Collections refer to a framework that


provides data structures and algorithms to store, retrieve, and manipulate
groups of objects efficiently.

This objects are often called items or Elements.

It allows developers to easily store ,retrieve and manipulate this groups of


objects.

Key Feautres of collections;

1 Unified Architecture: A unified architecture for representing and


manipulating collections.

2) Resizable Data Structures; Collections like ArrayList or HashSet


grow dynamically, unlike arrays.

3) Predefined Data Structures : predefined data sructures like like


ArrayList, HashMap, TreeSet, etc.

4) Algorithm Support : framework provides methods and algorithms for


sorting ,searching and manipulating collections efficiently.

##Collections Framework-is a group of classes and interfaces that


gives you pre-built ways to use common data structures .

The data structures can be lists ,sets ,maps ,queues etc.

Provides unified architecture for manipulating and storings groups of


objects.

Advantages-
1) Reusable Code -framework provides data structures and save
developers time and efforts.

2) Efficiency- it provides fast and optimized data structures for different


needs.

3) Consistency -all collections in the framework follow the same


interfaces and rules.

4) Scalability- it supports data structures that can handle large datasets


effectively.

Heirarchy of collection framework :


1) Utility package (java.util) contains all the classes and interfaces that
are required by the collection framework.
2) The collct, framw. Has an interface that provides an iterator to go
through all the collections.
3) Iterable interface is extended by the main collection interface ,which
is the root of the collection framework.

Difference between collection and Collections

Collection
Collections

Java.util.collection is an interface java.util.collections is a class

It is used to represent a group of objects it is used to define various


utility method for

As a single entity. For collection objects.

Rot interface of collection framework it is a utility class

Can be parametrized with a type. Not parametrized.

Implemented by various classes collections class itself cant


be installed.

List ,Queue,set etc Method like


sort(),reverse(),shuffle() etc.
List Interface

 List interface is a child of the collection interface.


 It is used to store data in a list format,where all items are kept in
order.
 It allows duplicate items and implemented by classes like
ArrayList ,vector and stack.

Key Characteristics of list Interface

 Ordered : list keep elements in the order you add them.


 Indexed :you can access elements by using their index number.
 Resizable :it can automatically grow or shrink in size.
 Searchable : You can search for an element and find its index .

Eg : List<String> names = new ArrayList<>();

names.add("Alice");

names.add("Bob");

System.out.println("List contents: " + names); // List contents:


[Alice, Bob, Alice]

System.out.println("First name: " + names.get(0)); //First name:


Alice

Explain Arraylist,Linkkedlist,vector,stack in reference to link


interface.

Or

Which classes implement link interface?

The classes that implements the list interface are :

ArrayList :

1) Implementation: arraylist is a dynamic array based


implementation of list interface.
2) Data Structure : it uses an array internally to store
elemnts,allowing quick acess and retrieval by index
3) Performance : Arraylist is fast for adding or accessing element by
index.
But inserting or dleting elements in the middle
is slow.
4) Use case : it is best for frequently access elemnts by index,size .

Eg :
ArrayList<String> colors = new ArrayList<>();

// Adding elements

colors.add("Red");

colors.add("Green");

colors.add("Blue");

// Accessing elements

System.out.println("First color: " + colors.get(0));

LinkedList :

1. Implementation : it uses a doubly likedlist structure.


2. Data Struct. : it consists of a sequence of elements, where each
elements is stored in a node that contain a refernce to the previous
and next elemnts in the sequence
3. Performance : it is fast for adding removing elemnts at start or
end. But finding elemnets by index is slow.
4. Use Case : good for frequently add or remove elemnts ,especially
at start or end of the list.

Eg;

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

// Adding elements

animals.add("Dog");

animals.add("Cat");

animals.add("Horse");

// Accessing elements

System.out.println("First animal: " + animals.get(0));

Vector :

1. Implementation : dynamic array that follows the lit interface.


2. Data structure : it uses array to store elemnts.
3. Performance ; vector works similar to arraylist but it is slower .
4. Use Cases : it good when multiple threads need to access or
change the list at the same time .

Eg:

Vector<String> cities = new Vector<>();

// Adding elements

cities.add("Delhi");

cities.add("Mumbai");

cities.add("Chennai");

// Accessing elements

System.out.println("First city: " + cities.get(0));

Stack :

1. Implementation : stack is a type of vector that follows a


LIFO order.
2. Data Struct(Operation) : mainly supports tow actions :
Push(add) and Pop(remove).
3. Performance : it performs like a vector , with a little extra
work for stack operations.
4. Use case : it is useful for tasks needing LiFo order ,like
solving math expressions ,backtracking.

Eg;

import java.util.Stack;

public class StackExample {

public static void main(String[] args) {

// Creating a Stack of Integers

Stack<Integer> stack = new Stack<>();

// Pushing elements onto the stack


stack.push(10);

stack.push(20);

stack.push(30);

// Popping the top element

int top = stack.pop();

System.out.println("Popped element: " + top);

// Peeking at the top element

System.out.println("Top element now: " + stack.peek());

// Printing all elements

System.out.println("Stack elements: " + stack);

Qns. Describe Queue Interface .which classes implemement


queue interface?

Queue Interface:

 The queue interface in java reprents a collection of element that


follows the FIFO principle.
 It extends the collection interface and includes methods for
adding ,removing and checking elemnts in queue.

Key Characteristics Queue interface:

1. FIFO ordering : elemnts are inserted at the end of the queue and
remove from the front.
2. Adding and removing elmnts : you can add items to the
end(using add()) and remove them from the front (using remove()).
3. Peeking :you can look at the item at the front without removing it
(using peek() ).
4. Size and empty check : method s to check the size of queue
(size() ) and wheter it is empty (isEmpty()).

Classes that implements the queue interface :


 LinkedList : it uses a doubly linked list to implement a queue.
 PriorityQueue : priorityQueue class implements the queue
interface using a priority heap.
 ArrayDeque : ArrayDequeu class implements the deque
interface,which extends the queue interface.

Describe set Interface .which classes implemement set interface?

Set Interface :

 The set interface in java represents a collection of unique elements.


 Meaning that no two elemnts in the set can be equal according to
the equal() method.
 It doesn’t allow duplicate elemnts.
 It doen’t maintain the insertion order of elemnts.

Key Characteristics of set interface :

1. Uniqueness : it contains only unique elements. Adding elemnts


which is already present in the set doesn’t change anything.
2. No ordering : It doen’t maintain the insertion order of elemnts.
3. Equality : elemnts in the set are compared for equality using the
equals() method.
4. No index : sets do not support index based access to elemnts. It
can only be access by looping or using search functions.

Classes that implements the set interface :

 Hashset : hashSet is a java class that implements the set interface


and stores items using a hash table.
 TreeSet : a TreeSet is a set that stores elemnts in a order using a
tree structure.
 LinkedHashSet : it is implementation of set interface that
maintains the insertion order of elemnts,in addition to ensuring
uniqueness.

HashSet :

 hashSet is a java class that implements the set interface and


stores items using a hash table
 it performs fast for adding ,removing and checking items,even
with large amounts of data.
 It does not keep elemnts in the order and not maintain insertion
order.

Key Features :

I. Uniqueness :it allows only unique items,so no duplicates are


stored.
II. Fast lookup : allows quick searching because it uses a hash table.
III. No order : elemnts in Hashset have no specific order.

LinkedhashSet:

 it is implementation of set interface that maintains the insertion


order of elemnts,in addition to ensuring uniqueness.
 It combines the features of Hashset with predictable iteration order.
 It uses both a Hash table and a linked list to keep track of the order
elemnts are added.

Key Features :

I. Uniqueness ; Ensures no duplicate elemnts like Hashset.


II. Efficient Lookup :Provides fast lookup while maintaining insertion
order.
III. Predictable order :elemnts are iterated in the order they were
added.

Queue Interface Set Interface


Represents a collection of elemnts It represents a collection of unique
with FIFO ordering. elemnts.
Maintain FIFO ordering No specific oredering.
Allow duplicates Doesn’t allow duplicate.
It implements linkedlist,priority It implements Hashset, Link
Queue Hashset,TreeSet.

SortedSet interface :

1. It is interface which keeps its elemnts in sorted order.


2. It extends the set interface and adds methods for accessing and
manipulating elemnts based on their sorted order.
3. It is useful for working with collectinos of uniwue elemnts that needs
to be sorted.
4. It provides quick access to elemnts based on their order.

Key Characteristics

 Sorted Order : elements are stored in a sorted way.


 Uniqueness : no duplicates elemnts are allowed.
 Efficient Retrieval: access to elemnts is efficient
 No index acess ; can’t acess elmetns by teir index.

TreeSet
 It is a java class that stores unique elements in a sorted order.
 It uses red black tree to keep elemnts sorted.
 Useful for situations wher you need a sorted collection of unique
elements.

Charecteristics

I. Sorted order : elements are kept in sorted order.


II. Unique elemnts : No duplicates are allowed.
III. Efficient operation : adding ,removing and accessing elemnts are
done efficiently
IV. Red black tree : it uses red black tree internally to maintain sorted
order

Map Interface

 It is used to store data as key-value pairs.


 It provides a way to store and retrieve elemnts based on their keys.
 Each key is unique and only it have one value.
 It is good for quickly accessing values using unique keys.

Charecteristics ;

I. Key Value pairs : maps store data as pairs of keys and values.
II. Unique Keys: each key must be in the map.
III. Efficient retrieval : maps allow quick access to values using their
keys.
IV. No ordering : Maps not keep their elements in any specific order.

You might also like