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

Collection Framework PDF

The document discusses Java collections and their use for storing and manipulating groups of objects, describing common interfaces like List, Set, Queue and Map and their implementing classes, and some key methods for each like add(), remove(), and get(). It also provides details on common collection classes like ArrayList, LinkedList, HashSet, TreeMap and how they handle aspects like ordering, duplicates and capacity.

Uploaded by

Suryateja Koka
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
131 views

Collection Framework PDF

The document discusses Java collections and their use for storing and manipulating groups of objects, describing common interfaces like List, Set, Queue and Map and their implementing classes, and some key methods for each like add(), remove(), and get(). It also provides details on common collection classes like ArrayList, LinkedList, HashSet, TreeMap and how they handle aspects like ordering, duplicates and capacity.

Uploaded by

Suryateja Koka
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Collection in Java

1. The collection in java is a framework that provides architecture to store and


manipulate the group of objects.
2. Java collection can achieve all the operations that can perform on a data such
as searching, sorting, insertion, deletion and manipulations.
3. Java collection framework provides many interfaces (sets, lists, queue,
dequeue) and classes (ArrayList, Vector, LinkedLists, PriorityQueue,
HashSet, LinkedHashSet,TreeSet).
4. The collection interface (java.util.collection) and map interface
(java.util.map) are the two main “root” interfaces of java collection classes.

Iterator is an interface that iterates that iterates the elements. It is used to traverse
the list and modify the elements. Iterator interface has three methods

• hasNext()
• next()
• remove()

Collection-> Root interface with basic method like add(), remove(),isEmpty(),


addAll()… etc.

Collection -> Parent

List set Queue Dequeue -> Child

Arrays

Suppose if we want to add 100 student details

int rollno=1;

int rollno2=2;
.

int rollno100=100;

*******

Array

Int rollno=new int[100];

Advantage

At a time we can store group of values

Disadvantages

1. stores same datatypes values


2. limits the size of the array

List

➔ It is an interface
➔ Can store Duplicate values and allows random access
➔ Elements are printed in an order.
➔ We can access any element from its index
➔ List are more likely array with dynamic length.
➔ Arraylist and linkedlists are the subclass used to implement list interface.
➔ List interface provides useful methods to add element at specific index,
remove, replace element based on index and to get a sub list using index.

Sets

➔ It is an interface
➔ Cannot store Duplicate values (ex: phone numbers)
➔ HashSet, TreeSet, LinkedHashSet are the Sub Class used to implement Set
interface-Parent class
➔ Doesn’t allow random access
➔ You can use iterator or foreach loop to traverse the elements of the set

Queue

➔ Typically ordered elements –FIFO order except exceptions like priority


queue
➔ All new elements are inserted at the tail of the queue.

Dequeue

➔ Elements can be inserted and removed at both ends.


➔ Allows both LIFO & FIFO.
➔ The name dequeue is short for “Double ended queue” and is usually
pronounced “deck”.

Methods in Lists

➔ It is a subinterface of collection.
➔ It contains index based method to insert and delete elements.

1. Void add(int index, E element)


2. Boolean add(E,e)-adds specific element at the end of a list.
3. Boolean addAll (Collection<? Extends E> C)
4. Boolean addAll(Index, Collection<? Extends E> C)
5. Void clear()
6. Boolean equals(object o)- compares
7. Object get(int index)
8. Boolean isEmpty()
9. Int lastIndexOf(object o)
10. Boolean contain(object o)
11. Object[] to array()
12. Int indexof(object o)
13. E remove(int index)
14. Boolean remove(object o)
15. Boolean removeall(Collection<? Extends E> C)
16. E set (int index, E element)
17. Void sort (Collection<? Super E> C)
18. List<E> sublist(int from index, int to index)
19. Int size()

Set
• Saves group of objects
• It is a non synchronized class
• Doesn’t allows duplicate values
• Doesn’t follow insertion order
• Doesn’t allow indexed based operations
• Set interface has

1. HashSet

2. LinkedHashSet

3. TreeSet

List
1. It maintains the order while insertion follows insertion order.
2. Allows duplicate values
3. Index based operations
HashSet LinkedHash TreeSet
Set
Duplicate no no no
Values
Insertion No-prints yes Ascending
order randomly order
(Sorting
order)
Synchroniz non non non
ed Synchroniz Synchronized Synchroniz
ed ed
Data Stores Hash Table Hash Table + Balanced
Double Tree
Linked List
Capacity 16 16 Based on
inserted
values it
will update
MAPS
Java.util.map;
Map is a Root interface.
Maps are not sub interfaces of collection.
Collection Map

List Set Queue Dequeue Sorted Map

Key + Value (pair) stores in map


(Key + Value) is called Entry
Example-( Pincode + City)
Dictionaries
There are 4 types of implementation classes
1. HashMap
2. LinkedHashMap
3. TreeMap
4. HashTable

Key value- doesn’t allows duplicate values (unique values)


Value- allows duplicate values
HashMap LinkedHashMap TreeMap HashTable

Doesn’t follow insertion Sorted order Doesn’t


follow order (Based on follow
insertion keys) insertion
order order
Non Non Non Synchronized
Synchronized Synchronized Synchronized
Duplicate Duplicate values
Duplicate Duplicate
values are not are not allowedvalues are not values are not
allowed for for keys allowed for allowed for
keys keys keys
Duplicate Duplicate values Duplicate Duplicate
values are are allowed for values are values are
allowed for values allowed for allowed for
values values values
Capacity-16 Capacity-16 No specific Capacity-11
size
Store sin hash Hash table+ Red Black Hash Table
table Double Linked Trees
list
Collection-> Interface

Collections-> implementation Class

Static Methods
sort()
reverse()
swap()
min()
max()

You might also like