UNIT - IV - Syllabus The Collections Framework (Java - Util)
UNIT - IV - Syllabus The Collections Framework (Java - Util)
Before Collections:
What is Collection?
Collection is a container object. It is used for storing homogeneous and heterogeneous, unique
and duplicate objects without size limitation and further it is used for sending objects at a time
from one class methods to other class methods as method arguments and return type with
single name across multiple layers of the project.
Classes
1. Container Objects classes
2. Cursor Objects classes
3. Utility Objects classes
<T> Represents generic type parameter, i.e which type of elements are being stored.
3. Utility Objects: it is nothing but a class that is given to perform different operations on
container objects are called utility objects i.e two classes
1. Collections class
2. Arrays
The Collection in Java is a framework that provides architecture to store and manipulate the
group of objects.
1. Collections are the containers that groups multiple items in a single unit
2. It provides architecture to store and manipulate a group of objects
3. Using collections various operations can be performed on the data like
searching,
sorting,
insertion,
manipulation,
deletion etc.
4. Java collection framework provide many Interfaces and classes
Collection Interfaces and class hierarchy
Iterable is the root interface of the Java collection classes. The Collection interface
extends Iterable interface, so all subtypes of Collection implement the Iterable interface.
This interface stands to represent data-structures whose value can be traversed one by one.
This is an important property.
Simple Hirarchy
Choosing the Righ Collection
Sets:
A set represents a group of elements arranged just like an array. The set will grow dynamically
when the elements are stored into it. A set will not allow duplicate elements. If we try to pass
for same element that is already available in the set, then it is not stored into the set.
List:
Lists are like sets. They store a group of elements. But lists allow duplicate values to be
stored.
Queues:
A Queue represents arrangement of elements in FIFO (First In First Out) order. This means
that an element that is stored as a first element into the queue will be removed first from the
queue.
Maps:
Maps store elements in the form of key and value pairs. If the key is provided then it’s
correspond value can be obtained. Of course, the keys should have unique values.
Remember, in all the cases the 'elements' refer to 'objects' only. This means we cannot store
primitive data types in the collection objects. We can store only objects since the main aim of
collections is to handle objects only, not the primitive data types.
1. For-each loop:
This is like for loop which repeatedly executes a group of statements for each element of
the collection. The format is:
for(variable : collection-object)
{
Statements:
}
Here, the variable assumes each element of the collection-object and the loop is executed as
many times as there are number of elements in the collection-object. If collection-object has n
elements the loop is executed exactly n times and the variable stores each element in each step.
class ForEachDemo{
public static void main(String args[]){
12
13
14
44
2. Iterator Interface: Iterator is an interface that contains methods to retrieve the elements one
by one from a collection object. It retrieves elements only in forward direction. It has 3
methods:
4. Enumeration Interface: This interface is useful to retrieve elements one by one like
Iterator. It has 2 methods.
HashSet Class
HashSet Class: HashSet represents a set of elements (objects). It does not guarantee the
order of elements. Also it does not allow the duplicate elements to be stored.
We can write the HashSet class as : class HashSet <T>
We can create the object as : HashSet <String> hs = new HashSet<String> ();
HashSet();
HashSet (int capacity); Here capacity represents how many elements can be stored
into the HashSet initially. This capacity may increase automatically when more
number of elements is being stored.
Program : Write a program which shows the use of HashSet and Iterator.
package com.myPack;
import java.util.HashSet;
import java.util.Iterator;
package com.myPack;
import java.util.*;
class Stack1
{
int top = -1, st[]=new int[5];
int pop(){
return(st[top--]);
}
void display()
{
System.out.println("\nStack elements from top to
bottom\n");
for(int i=top;i>=0;i--)
System.out.println(st[i]);
}
boolean isFull(){
return(top==5-1);
}
boolean isEmpty(){
return(top==-1);
}
}
public class Stack {
while(ch != 4) {
System.out.println("\n1.PUSH\n2.POP\n3.DISPLAY\n4.EXIT");
System.out.println("ENTER YOUR CHOICE");
ch=sc.nextInt();
switch(ch){
case 1 : if(s.isFull())
System.out.println("\nstack is full");
else {
System.out.println("Enter element");
el=sc.nextInt();
s.push(el);
}break;
case 2: if(s.isEmpty())
System.out.println("\nstack is empty");
else {
el=s.pop();
System.out.println("\nDeleted element =
"+el);
}break;
case 3: if(s.isEmpty())
System.out.println("\nstack is empty");
else
s.display();
break;
case 4: break;
OUTPUT:
1.PUSH
2.POP
3.DISPLAY
4.EXIT
ENTER YOUR CHOICE
1
Enter element
10
1.PUSH
2.POP
3.DISPLAY
4.EXIT
ENTER YOUR CHOICE
1
Enter element
20
1.PUSH
2.POP
3.DISPLAY
4.EXIT
ENTER YOUR CHOICE
1
Enter element
30
1.PUSH
2.POP
3.DISPLAY
4.EXIT
ENTER YOUR CHOICE
3
30
20
10
1.PUSH
2.POP
3.DISPLAY
4.EXIT
ENTER YOUR CHOICE
1
Enter element
40
1.PUSH
2.POP
3.DISPLAY
4.EXIT
ENTER YOUR CHOICE
3
40
30
20
10
1.PUSH
2.POP
3.DISPLAY
4.EXIT
ENTER YOUR CHOICE
1
Enter element
50
1.PUSH
2.POP
3.DISPLAY
4.EXIT
ENTER YOUR CHOICE
3
50
40
30
20
10
1.PUSH
2.POP
3.DISPLAY
4.EXIT
ENTER YOUR CHOICE
2
Deleted element = 50
1.PUSH
2.POP
3.DISPLAY
4.EXIT
ENTER YOUR CHOICE
ArrayList Class
ArrayList Class: An ArrayList is like an array, which can grow in memory dynamically.
ArrayList is not synchronized. This means that when more than one thread acts simultaneously
on the ArrayList object, the results may be incorrect in some cases.
ArrayList class can be written as:
package com.myPack;
import java.util.ArrayList;
import java.util.Iterator;
Output:
Asia
Australia
Antarctica
North America Carona
Carona
South America
Africa
Europe
Program on ArrayList
LinkedList Class: A linked list contains a group of elements in the form of nodes. Each
node will have three fields- the data field contatins data and the link fields contain
references to previous and next nodes.A linked list is written in the form of:
class LinkedList<E>
we can create an empty linked list for storing String type elements (objects) as:
Note: In case of LinkedList counting starts from 0 and we start counting from 1.
Program : Write a program that shows the use of LinkedList class.
package com.myPack;
import java.util.LinkedList;
Output: