0% found this document useful (0 votes)
5 views21 pages

Collections

The document provides an overview of the Collections Framework in Java, highlighting the differences between arrays and collections, as well as various collection types such as List, Set, and Map. It explains the characteristics, advantages, and limitations of different collection classes like ArrayList, LinkedList, Vector, and HashSet. Additionally, it discusses specific methods and constructors associated with these collections, emphasizing their use cases and performance considerations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views21 pages

Collections

The document provides an overview of the Collections Framework in Java, highlighting the differences between arrays and collections, as well as various collection types such as List, Set, and Map. It explains the characteristics, advantages, and limitations of different collection classes like ArrayList, LinkedList, Vector, and HashSet. Additionally, it discusses specific methods and constructors associated with these collections, emphasizing their use cases and performance considerations.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 21

COLLECTIONS

By- Harshwardhan Zade


 Collections Framework :
 An array is an indexed collection of fixed number of homogeneous data elements.

 The main advantage of Array is we can represent multiple values by using Single variable. So that
readability of the code will be improved.

 Limitations of Arrays:

 Array are fixed in size . i.e. once we create an Array there is no chance of increasing or
decreasing the size based on our requirement. Due to this , to use Arrays concept compulsory we
should know the size in advance. Which may not be possible always
 Array can hold homogenous datatype elements.
We can solve this problem by using object type array
Object [] a= new Student ();
a[1]=new Customer ();
 Collections :

• collections are grow able in nature I .e., Based on our requirement we can
increase or decrease the size.

• Collections can hold both homogenous and heterogeneous elements objects.

• Every collection class is implemented based on some standard Data Structure


hence for every requirement we have to write code explicitly which increases
complexity of the program.

• being a programmer ,we are responsible to use those methods, we are not
responsible to implement those methods
Collections :

s1 s2 Individual objects

s3 s4
Single entity
s5

If we want to represent group of individual objects as a single entity then we should go for
collection.
 Collection Framework :

• it contains several classes and interfaces which can be used to represent group of individual
objects as a single entity.

Key interfaces of Collections framework:

Collection List Set SortedSet

NavigableSet Queue Map Sortedmap NavigableMap

Collection (i) //Collection interface

• if we want to represent a group of individual objects as a single entity then we


should go for Collection(I).

• Collection interface defines the most common methods which are defined for any collection
object.
Difference between Collection and Collections :

 List Interface :
 list is the child interface of collection.

 if we want to represent a group of individual object as a single entity where duplicates


are allowed and insertion order must be preserved then we should go for list.

 We can preserve insertion order via index or with index and we can differentiate
duplicate objects by using index. Hence, index will play an important role in List.
List Interface also has its own specific methods. They are:
• void add ( int index, Object O);
• boolean addAll( int index, Collection C);
• Object get( int index );
• Object remove (int index );
• Object set(int index, Object new)
 To replace the element present at specific index with provided objects and returns old object
int indexOf(Object O);
ListIterator listIterator();
 Array List (i c)
The underlying Data structure is resizable array or growable array.
 Duplicates are allowed
 Insertion order is preserved
 Heterogenous object are allowed (Except TreeSet & TreeMap) everywhere heterogeneous
object are allowed.
Null insertion is allowed
 Constructor:
Creates an empty Array list objects with default initial capacity 10, once Array list reaches its
maximum capacity then a new array list object will be created with given default capacity 10.
ArrayList l=new ArrayList(int initial Capacity);
create an empty ArrayList object with specified initial capacity
ArrayList l=new ArrayList(Collection c);
Linked List
• The underlying Data Structure is double link list //circular list
• Insertion order is persevered.
• Duplicate objects allowed
• Heterogenous object allowed
• Null insertion is possible
• Linkedlist implements Serializable and cloneable interfaces But not random access.
• Linkedlist is best Choice if our frequent operation is insertion or deletion In the middle.
• Linked list is the worst choice if our frequent operation is retriable.
LinkedList Constructor, Methods
1> LinkedList l=new Linkedlist();
create an empty Linkedlist object
2> LinkedList l=new Linkedlist(collection c);
create an equivalent LinkedList object for the given collection
 Methods
1> Void addfirst (object o); 2> void addlast (object o);

3> Object getfirst (); 4> Object getlast ();


5> Object removeFirst (); 6> Object removelast ();
Vector(i c)
• The underlying Data Structure is resizable Array or growable Array.
• Insertion order is preserved.
• duplicate are allowed
• heterogeneous object are allowed
• null insertion is possible.
• It implements serializable and cloneable and random access interface
• Every method present in vector is synchronized and hence vector object are thread safe
Vector constructor and methods
1> vector v=new vector();
Create an empty vector object with default capacity 10.
2> vector v =new vector(int initial capacity);
Creates an empty vector object with initial capacity
3> vector v =new vector (int initial capacity ,int incremental capacity)
When capacity is full it creates a new object with the specified capacity
4> Vector v =new Vector(Collection c);
 add element(obj o); to add object
 Remove element (object o); ,remove element At();, remove all element  to remove
 Object element at(int index),object first element(),object last element()to get
Arrayalist Vector ArrayList LinkedList
Every method Every method ArrayList is the best LinkedList is the
presence in ArrayList presence in vector is choice if our best choice if our
is non-synchronize. synchronize frequent operation frequent operation
At a time multiple At a time only one relivable operations. is insertion or
thread are allowed to thread are allowed to deletion in the
operate an array list operate an vector middle
object and hence it is object and hence it is ArrayList is the worst LinkedList is the
not thread safe thread safe. choice if our worst choice if our
Relatively Relatively frequent operation is frequent operation
performance is high performance is low insertion or deletion relivable operations.
because thread are because thread are in the middle
not required to wait not required to wait in ArrayList the in Linkedlist the
to operate on to operate on element will be element will not be
ArrayList object. ArrayList object. element will be stored in
Introduced in 1.2 Introduced in 1.0 stored in conjunctive conjunctive memory
version Means non version Means it is and hence retrieval location and hence
legacy legacy option is easy. retrieval option will
be complex.
Stack
• It is a child class of vector.
• It is a specially designed class for last in first out order(LIFO).
• Constructor of stack
1> Object push(Object o);  to insert an object into the stack
2> Object pop();  to remove an return top of the stack.
3> Object peek(); to return top of the stack without removal
4> boolean empty(); returns true if the stack is empty
5> int search(Object 0);  returns offset if the element is available otherwise returns -1.
Three cursors of java
• If we want to get objects one by one from the collections then we should go for cursors.
Set interface
• Set is the child interface of collection.
• if we want to represent a group of individual object as a single entity where
duplicate are not allowed and insertion order not preserved.
• set interface doesn’t contain any new method and we have to use only
collection interface methods.
Hash set
• The underlying data Structure is HashTable.
• Duplicate objects are not allowed.
• Insertion order is not preserved.
• It is based on Hash Code of the object.
• Null insertion is possible (only once)
• Heterogenies object are allowed.
• Implementation Serializable and cloneable but not random-access interface.
• HashSet is the best choice if our frequent operation is searching operation.
 LinkedHashSet
• It is the child class of the HashSet.
• It is exactly same as HashSet Including constructor and method except the
following difference.

HashSet LinkedHashSet

The underlying Data Structure is Underlying Data Structure is


HashTable Combination of Linkedlist and
HashTable.

Insertion order not preserved Insertion order preserved

Introduction in 1.2 version Introduced in 1.4 version


SortedSet
• Sorted set is the child interface of set.
• If we want to represent a group of Individual object according to some sorting order without
duplicate then we should go for sorted set.
• Methods
1> Object first(); returns first element of the SortedSet.
2> Object last(); returns last element of the SortedSet.
3> SortedSet headset(object obj) ; returns sorted set whose element are< object
4> SortedSet tail Set(object obj); returns SortedSet where elements are >=object
5> Sorted subset(object obj1, object obj2); Returns SortedSet whose elements are >=object 1
and<object 2
TreeSet(I c)
• The underlying Data Structure is Balanced tree.
• Duplicate object are not allowed.
• Insertion order not preserved.
• Heterogenous object are not allowed other wise we will get runtime exception saying
class cast exception.
• Null insertion possible (only once).
• Tree Set implement serlilzabale and cloneable not random access.
• All objects insertion based on some shorting order it may be default natural shorting
order or customise shorting order.

You might also like