COLLECTION FRAMEWORK
Collections is a framework provided by java.
By using collection frame work we can able to store different types of values in single
object.
We can able to perform searching and sorting operations easily by using predefined
methods and by implementing less lines of coding.
Collection is nothing but group of objects, and these objects are of different data types
stoting in one single object.
CURSORS :-
Iterator ( I ) → Universal cursor
ListIterator ( I ) → only works for lists
Enumeration ( I ) → Legacy cursor
DIFFERENCE BETWEEN ARRAYS AND COLLECTION.
ARRAYS COLLECTION
1. Arrays are fixed in size. 1. Collection is growable in nature.
2. We can store only homogeneous kind 2. We can able to store both
of data. homogeneous and heterogeneous kind
of data.
3. Here we can deal with both primitive 3. Here we can deal with only object
and object type of data. types of data.
Collection framework is mainly classified into two types.
1. New collection framework.
2. Legacy collection framework.
New Collection frame work :-
New collection framework is again classified into two types.
1. 1D collection framework
2. 2D collection framework
1D new collection framework contains different types of interfaces and classes, by using 1D
new collection framework we can able to store only values but not key value pairs.
Note :-
All predefined classes and interfaces of entire collection framework are available in a
predefined package called “ UTIL ”.
DIFFERENCE BETWEEN COLLECTION, LIST AND SET.
COLLECTION LIST SET
1) It is super interface of both 1) It is a sub interface of 1) It is also a sub interface of
list and set. collection interface. collection interface.
2) Here we can store duplicate 2) Here we can store 2) Here we can not store
elements. duplicate elements. duplicate elements.
3) Here there will be no order 3) Here there will be order 3) Here there will be no order
of elements. of elements. of elements.
4) Here we can able to retrieve 4) Here we can able to 4) Here we can able to retrieve
the elements only in forward retrieve the elements in the elements only in
direction. both forward and forward direction.
backward direction.
5) Here we can able to add the 5) Here we can able to add 5) Here also we can add the
elements only at ending of the elements anywhere elements only at the ending
the index. based on requirement. of the index.
Note :- Null values can be stored in collection, list and set interfaces.
COLLECTION :-
It is a predefined Interface present in “ UTIL “ package the fully qualified name of
collection Interface is “ JAVA.UTIL.COLLECTION “.
Here it does not follow any order of elements and it will allow duplicates.
If you want to represent a group of individual objects as a single Entity then we
should go for collections.
Collection interface defines the most common methods which are applicable for any
collection object.
There is no concrete class which implements collection class directly.
METHODS :-
1. public boolean add (object)
2. public int size ( )
3. public boolean isEmpty ( )
4. public boolean addAll (collection)
5. public boolean remove (object)
6. public boolean removeAll (collection)
7. public boolean equals (object)
8. public void clear ( )
9. public object[ ] toArray ( )
10. public boolean contains (Object)
11. public boolean containsAll (collection)
12. public Iterator iterator ( )
USE AND DESCRIPTION ABOUT METHODS :-
1. boolean add (object) → Appends the specified element to the end of this list
2. int size ( ) → To get the number of elements in the list
3. boolean isEmpty ( ) → To check if list is empty or not
4. boolean addAll ( collection ) → Appends all the element to another list
5. boolean remove (object) → Removes the first occurrence of the specified element from this list
6. boolean removeAll (collection) → It is used to remove the common elements that are
common in two lists
7. boolean equals (object) → Returns true if the object matches to the object in the list
8. void clear ( ) → Removes all the elements from the list
9. object[ ] toArray ( ) → Returns an array containing all of the elements in this list in proper
sequence
10. boolean contains ( object ) → Returns true if this list contains the specified element
11. boolean containsAll ( collection ) → Returns true if this list contains the same elements
12. Iterator iterator ( ) → Returns an iterator over the elements in this list in proper sequence
ITERATOR ( I ) :-
It is a predefined Interface available in a predefined package like “ java.util ”
package, By using this interface we can able to retrieve the values in forward
direction and we can remove the values also.
The iterator will works like cursor, we need to make the cursor position move from
one index into another index to get the elements.
METHODS :-
1. public boolean hasNext ( )
2. public object next ( )
3. public void remove ( )
USE AND DESCRIPTION ABOUT METHODS :-
1. boolean hasNext ( ) → Returns true if there exists an element in the collection uses when
retrieving the elements in forward direction.
2. Object next ( ) → Returns the next element of the collection
3. void remove ( ) → Removes the last element returned by the next ( )
For creating Object for Iterator
Iterator itr = CollectiobObject . iterator ( );
while ( itr . hasNext ( ))
{
System.out.print ( itr . next ( ));
}
LIST :-
List is child interface of collection.
If we want to represent a group of individual objects as a single object where
duplicates are allowed and insertion order must be preserved.
METHODS :-
1. public int size ( )
2. public boolean isEmpty ( )
3. public boolean add (object )
4. public boolean add (int,object)
5. public boolean addAll (collection)
6. public boolean addAll ( int, collection )
7. public void remove (object)
8. public boolean remove ( int )
9. public boolean removeAll ( Collection )
10. boolean retainAll ( Collection )
11. public Object set ( int, object )
12. public boolean equals (object )
13. public boolean indexof ( object )
14. public int lastIndexOf ( Object )
15. public void clone ( )
16. public void clear ( )
17. public Iterator iterator ( )
18. public ListIterator listIterator ( )
19. public Object [ ] toArray ( )
20. public Object get ( int )
21. public boolean contains ( object )
22. public boolean containsAll ( collection )
23. public List subList ( int from index, int to index )
USE AND DESCRIPTION ABOUT METHODS :-
1. int size ( ) → To get the number of elements in the list
2. boolean isEmpty ( ) → To check if list is empty or not
3. boolean add (object ) → Appends the specified element to the end of this list
4. boolean add (int,object) → Appends the specified element at that given index of this list
5. boolean addAll (collection) → Appends all the element to another list
6. boolean addAll ( int, collection ) → Appends all the element to another list at specified
position of the list.
7. boolean remove (object) → Removes the first occurrence of the specified element from this
list.
8. boolean remove ( int ) → Removes the element in that particular index.
9. boolean removeAll ( collection Object ) → It is used to remove the common elements that
are common in two lists.
10. boolean retainAll ( Collection Object ) → Retains only the elements that are common in
both lists.
11. Object set ( int, object ) → Replaces the element at the specified position in the list with the
specified element.
12. boolean equals ( Collection object ) → Returns true if the elements in Object1 matches to
the elements in that particular index of Object2 if not matches returns false.
( [“First”, 2, true, ‘A’] ) → Object1
( [“First”, 2, true, ‘A’] ) → Object2
( [2, “First”, true, ‘A’] ) → Object3
13. int indexof ( object ) → It is used to get the index value of the specific element in the list.
14. int lastIndexOf ( Object ) → It is used to get the last occurrence index value of the specific
element in the list.
15. void clear ( ) → Removes all the elements from the list
16. void clone ( ) → It is used to return a shallow copy of an arrayList.
17. Iterator iterator ( ) → Returns an iterator over the elements in this list in proper sequence.
18. ListIterator listIterator ( ) → Returns a list iterator over the elements in the list.
19. Object [ ] toArray ( ) → Returns an array containing all of the elements in this list in proper
sequence.
20. Object get ( int ) → Returns the element at the specified position in the list.
21. boolean contains ( object ) → Returns true if the list contains the specified element else
returns false.
22. boolean containsAll ( collection ) → Returns true if this list contains the same elements.
23. List subList ( int from index, int to index ) It is used to fetch all the elements with in the
given range.
LIST ITERATOR ( I ) :-
It is a predefined Interface available in “ UTIL “ package, The main purpose of this interface
is used to retrieve the elements in both forward and backward direction and it is also works
like cursor.
METHODS :-
1. public boolean hasNext ( )
2. public object next ( )
3. public void remove ( )
4. public boolean hasPrevious ( )
5. public object previous ( )
USE AND DESCRIPTION ABOUT METHODS :-
1. boolean hasNext ( ) → Returns true if there exists an element in the collection and uses when
retrieving the elements in forward direction
2. object next ( ) → Returns the next element of the collection
3. void remove ( ) → Removes the last element returned by the next ( )
4. boolean hasPrevious ( ) → Returns true if there exists an element in the collection and uses
when retrieving the elements in backward direction
5. object previous ( ) → Returns the next element of the collection
For Creating Object for List Iterator
ListIterator li = collectionobject . listIterator ( );
ARRAY LIST :-
It is a predefined class present in “ UTIL ” package, It is the implementation class of
list interface.
It will allow both null and duplicate values.
It will follow insertion order of the elements.
It will use dynamic array for storing and retrieving of elements, array size is fixed but
Array list size is flexible and growable.
It is a non-Synchronized class and the default capacity of array list is 10.
CONSTRUCTORS :-
ArrayList ( )
ArrayList ( int )
ArrayList ( collection )
METHODS :-
We can use all the methods present in list interface.
1. Create a java application where we need to create object for array list then add different
types of values and then retrieve the values in forward direction then remove few values
and retrieve the elements again.
import java.util.*;
public class Arraylist {
public static void main(String[] args) {
ArrayList arraylist1=new ArrayList();
arraylist1.add(1);
arraylist1.add("JAMES");
arraylist1.add(true);
arraylist1.add(12.43f);
arraylist1.add(676743.546);
arraylist1.add('A');
Object arr[]=arraylist1.toArray();
for(Object k:arr)
{
System.out.print(k+" ");
}
System.out.println();
ArrayList arraylist2=new ArrayList(arraylist1);
arraylist2.add(10);
arraylist2.add("CSS");
Iterator itr=arraylist2.iterator();
while(itr.hasNext())
{
System.out.print(itr.next()+" ");
}
System.out.println();
arraylist2.remove(12.43);
arraylist2.remove("JAMES");
Iterator itr1=arraylist2.iterator();
while(itr1.hasNext())
{
System.out.print(itr1.next()+" ");
}
}
}
OUT PUT :-
1 JAMES true 12.43 676743.546 A
1 JAMES true 12.43 676743.546 A 10 CSS
1 true 12.43 676743.546 A 10 CSS
2. Create a java application where we need to create array list object, add only integer type
of values by using generic type as integer then display all these values by using iterator,
Create second object for array list class then add only string type of values using generic
then display all these values by using for each loop.
import java.util.*;
public class Arraylist {
public static void main(String[] args) {
ArrayList<Integer> arraylist1=new ArrayList<>();
arraylist1.add(1);
arraylist1.add(2);
arraylist1.add(3);
arraylist1.add(4);
arraylist1.add(5);
arraylist1.add(6);
Object arr[]=arraylist1.toArray();
for(Object k:arr)
{
System.out.print(k+" ");
}
System.out.println();
ArrayList<String> arraylist2=new ArrayList<>();
arraylist2.add("JAMES");
arraylist2.add("CSS");
arraylist2.add("SQL");
Object arr1[]=arraylist2.toArray();
for(Object k:arr1)
{
System.out.print(k+" ");
}
}
}
OUT PUT :-
123456
JAMES CSS SQL
LINKED LIST :-
It is a predefined class present in “ UTIL ” package.
It is the implementation class of list interface.
It will follows insertion order of elements.
It will use doubly linked list for sorting the elements by default.
It will stores the data in the form of node, each node contains both data and address.
Whatever the node contains the address, this is the actual address of the next node.
It will allow both duplicate and null values.
It is a non-synchronized class.
CONSTRUCTORS :-
LinkedList ( )
LinkedList ( collection )
METHODS :-
1. public int size ( )
2. public boolean isEmpty ( )
3. public boolean add ( object )
4. public boolean add ( int, object )
5. public boolean addAll ( collection )
6. public boolean addAll ( int, collection )
7. public void set ( int, Object )
8. public boolean remove ( object )
9. public object get ( int )
10. public Boolean removeAll ( collection )
11. public void clear ( )
12. public boolean contains ( object )
13. public boolean equals ( object )
14. public boolean containsAll ( collection )
15. public void addFirst ( object )
16. public void addLast ( object )
17. public Iterator iterator ( )
18. public ListIterator listIterator ( )
19. public Object getFirst ( )
20. public Object getLast ( )
21. public Object removeFirst ( )
22. public Object removeLast ( )
23. public Object [ ] toArray ( )
24. public Object peek ( )
25. public Object peekFirst ( )
26. public Object peekLast ( )
27. public Object pollFirst ( )
28. public Object pollLast ( )
29. public void push ( Object )
30. public Object pop ( )
31. public boolean offer ( object )
32. public boolean removeFirstOccurence ( Object )
33. public boolean removeLastOccurence ( Object )
USE AND DESCRIPTION ABOUT METHODS :-
1. int size ( ) → To get the number of elements in the list.
2. boolean isEmpty ( ) → To check if list is empty or not.
3. boolean add (object ) → Appends the specified element to the end of this list.
4. boolean add (int,object) → Appends the specified element at that given index of this list.
5. boolean addAll (collection) → Appends all the element to another list.
6. boolean addAll ( int, collection ) → Appends all the element to another list at specified
position of the list.
7. void set ( int, Object ) → Replaces the element at the specified position in the list with the
specified element.
8. boolean remove ( object ) → Removes that particular element from the list.
9. Object get ( int ) → It returns the element present in that index.
10. boolean removeAll ( collection ) → It is used to remove the common elements that are
common in two lists.
11. void clear ( ) → Removes all the elements from the list.
12. boolean contains ( object ) → Returns true if the list contains the specified element else returns
false.
13. boolean equals ( object ) → Returns true if the object matches to the object in the list.
14. boolean containsAll ( collection ) → Returns true if the list contains the same elements else
returns false.
15. void addFirst ( object ) → Appends the specified element to the first of the list.
16. void addLast ( object ) → Appends the specified element to the last of the list.
17. Iterator iterator ( ) → Returns an iterator over the elements in this list in proper sequence.
18. ListIterator listIterator ( ) → Returns a list iterator over the elements in the list.
19. Object getFirst ( ) → Retrieves the first element in the list.
20. Object getLast ( ) → Retrieves the last element in the list.
21. Object removeFirst ( ) → Removes and retrieves the first element in the list.
22. Object removeLast ( ) → Removes and retrieves the last element in the list.
23. Object [ ] toArray ( ) → Returns an array containing all of the elements in this list in proper
sequence.
24. Object peek ( ) → The head of the list is retrieved and it is not going to affect any element of the
list.
25. Object peekFirst ( ) → The first element of the list is retrieved and it is not going to affect any
element of the list.
26. Object peekLast ( ) → The last element of the list is retrieved and it is not going to affect any
element of the list.
27. Object pollFirst ( ) → The first element of the list is retrieved and removed from the list.
28. Object pollLast ( ) → The last element of the list is retrieved and removed.
29. void push ( Object ) → Pushes the specified element to the first of the list.
30. Object pop ( ) → The last element of the list is popped/ removed.
31. boolean offer ( object ) → Appends the element to the last of the list.
32. boolean removeFirstOccurence ( Object ) → Removes the element where it is first
occurred from the list.
33. boolean removeLastOccurence ( Object ) → Removes the element where it is last occurred.
1. Create a java application where we need to need to create Object of linked list add
different types of values, Identify number of elements available in linked list object, get
the values by using list iterator, remove top of the element and bottom of the element and
then display remaining values by using iterator.
import java.util.*;
public class Arraylist {
public static void main(String[] args) {
LinkedList ll=new LinkedList();
ll.add(100);
ll.add(20);
ll.add(135);
ll.add(45);
ll.add(120);
ListIterator li=ll.listIterator();
while(li.hasNext())
System.out.print(li.next()+" ");
System.out.println();
ll.pollFirst();
ll.pollLast();
Iterator li1=ll.iterator();
while(li1.hasNext())
System.out.print(li1.next()+" ");
}
}
OUTPUT :-
100 20 135 45 120
20 135 45
DIFFERENCE BETWEEN ARRAY LIST AND LINKED LIST
ARRAY LIST LINKED LIST
1. The internal data structure for array 1. The internal data structure for linked
list is dynamic array. list is double linked list.
2. Insertion and deletion operations at the 2. It provides efficient insertion and
end of the list are effective / effective removal operations at both the
but inserting or removing of elements beginning and end of the list.
from the middle of the list requires
shifting sub sequent elements.
3. Time complexity of inserting or 3. Time complexity for insertion and
retrieving elements in the middle of the removal operations at the beginning or
list takes O(n) time complexity due to end of the list are faster taking O(1)
shifting. time complexity.
4. It requires less memory space. 4. It requires more memory space because
it needs to store references to previous
and next elements.
5. To retrieve the elements it takes O(n) 5. To retrieve the elements it takes O(n)
time complexity. time complexity.