0% found this document useful (0 votes)
27 views16 pages

Collections Framework & Generics

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

Collections Framework & Generics

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

Java By Venkatesh Mansani Naresh i Technologies

Collections framework
& Generics
Collections Framework:
A set of collection classes and interfaces is called as collections framework.
(or)
A set of data structures related classes and interfaces is called as collections
framework.

Collection:
A collection is an object that represents group of objects.

Data Structures:
Arranging data in different formats is called data structures.

Advantages of Collections Framework:


1) Reduces programming effort
2) Increases programming speed & quality
3) Allows interoperability among unrelated APIs.
Collection classes & interfaces are the part of java.util package.
java.util package classes & interfaces are divided into two categories:
1) Collections Framework Collections
2) Legacy Collections

1) Collections Framework Collections:


JDK 1.2 & above versions collection classes & interfaces are called Collections
framework collections.

[email protected] Naresh i Technologies


Java By Venkatesh Mansani Naresh i Technologies

2) Legacy Collections:
JDK 1.0 & 1.1 versions collection classes & interfaces are called legacy collections.
Collections Framework Collections are divided into 3 sub categories:
1) Core Collection Interfaces
2) General Purpose Implementations
3) More Utility Collections

1) Core Collection Interfaces:


These interfaces are the foundation of collections framework.
1) Collection
2) List
3) Set
4) Map
5) SortedSet
6) SortedMap
7) NavigableSet
8) NavigableMap
9) Queue
10) Deque

1) Collection interface:
It is a root interface in a one dimensional collections hierarchy.

2) List interface:
It extends Collection interface and it maintains sequences. It allows duplicate
elements.

[email protected] Naresh i Technologies


Java By Venkatesh Mansani Naresh i Technologies

3) Set interface:
It extends Collection interface and it maintains sets. It does not allow duplicate
elements.

Differences between List & Set:


List Set
1) It maintains sequences. 1) It maintains sets.
2) It allows duplicates. 2) It does not allow duplicates.

4) Map interface:
It is a root interface in a two dimensional collections hierarchy. It maintains data
as a key/value pairs. It does not allow duplicate keys(Values may be duplicated).

Differences between Set & Map


Set Map
1) It is a one dimensional 1) It is a two dimensional
collection interface collection interface.
2) It contains elements. 2) It contains key/value pairs.
3) It does not allow duplicates 3) It does not allow duplicate keys.
(Values may be duplicated).
4) It is an index based 4) It is a key based collection.
Collection.

5) SortedSet interface:
A SortedSet is a Set in which elements are sorted. It extends Set interface.

6) SortedMap interface:
A SortedMap is a Map in which key/value pairs are sorted based on keys. It
extends Map interface.

[email protected] Naresh i Technologies


Java By Venkatesh Mansani Naresh i Technologies

7) NavigableSet interface:
It is used to navigate elements of a Set. It extends SortedSet interface.

8) NavigableMap interface:
It is used to navigate elements of a Map. It extends SortedMap interface.

9) Queue interface:
It is called as First In First Out(FIFO) list. It allows insertion at rear end and
deletion at front end.

10) Deque interface:


It is called as Double Ended QUEue data structure. It allows both insertion &
deletion at both the ends(front end & rear end).

Differences between Queue & Deque:


Queue Deque
1) It is a queue data structure 1) It is a double ended queue data structure
2) It is called as First In First 2) It is not called as First In Out List
First Out List
3) It allows insertion at rear 3) It allows insertion at end only.
both the ends.
4) It allows deletion at front 4) It allows deletion at
end only. both the ends.

2) General Purpose Implementations:


The core collection interfaces implementation classes are called general purpose
implementations.
1) ArrayList
2) LinkedList
3) HashSet

[email protected] Naresh i Technologies


Java By Venkatesh Mansani Naresh i Technologies

4) LinkedHashSet
5) TreeSet
6) HashMap
7) LinkedHashMap
8) TreeMap
9) PriorityQueue
10) ArrayDeque

1) ArrayList class:
It is an array representation of list implementation class.
It allows duplicate elements because it implements List interface.
It is an implementation linear list data structure.
It supports all the operations of linear list data structure.
It supports all types of data.
It allows null values also

Generics:
Generics allows to write type safe programs.
Generics are introduced in JDK 1.5 version in 2004.

Advantages of generics:
1) Allows to write type safe programs
2) It does not require type casting
The syntax to create an object to generic class:
ClassName<ReferenceDataType> ObjectReference =
new Constructor<ReferenceDataType>();

[email protected] Naresh i Technologies


Java By Venkatesh Mansani Naresh i Technologies

Examples:
1) ArrayList<Integer> al1=new ArrayList<Integer>();
The above ArrayList object is type safe because it allows only integer type
elements.
2) ArrayList<String> al2=new ArrayList<String>();
The above ArrayList object is type safe because it allows only string type
elements.
3) ArrayList<Float> al3=new ArrayList<Float>();
The above ArrayList object is type safe because it allows only float type elements.

Generic Type Inference:


This feature allows to create an object to generic class in a new way. This feature
introduced in JDK 1.7 verion in 2011.

Example:
ArrayList<Integer> al=new ArrayList<Integer>();
The above code can be written from JDK 1.7 onwards as follows:
ArrayList<Integer> al=new ArrayList<>();

2) LinkedList class:
It is linked representation of list implementation class.
It allows duplicate elements because it implements List interface.
It is an implementation double linked list data structure.
It supports all the operations of double linked list data structure.
It supports all types of data.
It allows null values also

[email protected] Naresh i Technologies


Java By Venkatesh Mansani Naresh i Technologies

Differences between ArrayList & LinkedList


ArrayList LinkedList
1) It is an array representation 1) It is a linked representation of list
implementation class of list implementation class
2) It is a linear list data 2) It is a double linked list data structure
structure.
3) It occupies less memory 3) It occupies more memory
because data stored in nodes.
4) In ArrayList, insertion & 4) In LinkedList, it does not
deletion operations require require shuffling of data.
shuffling of data.

3) HashSet class:
It is an implementation of hashing technique with array representation.
Hashing is a technique, in which insertion, deletion & find operation in a constant
average time.
It does not allow duplicate elements because it implements Set interface.
It supports all types of data.
It allows null values also.
It is an unordered set.

4) LinkedHashSet class:
It is an implementation of hashing technique with linked representation.
It does not allow duplicate elements because it implements Set interface.
It supports all types of data.
It allows null values also.
It is an ordered set.

[email protected] Naresh i Technologies


Java By Venkatesh Mansani Naresh i Technologies

5) TreeSet class:
It is an implementation of binary search technique with linked representation.
A binary tree is said to be binary search tree if it is follows the following rules.
1) If the element is less than root element, then it must be left sub tree.
2) If the element is greater than root element, then it must be right sub tree.
It does not allow duplicate elements because it implements Set interface.
It supports all types of data.
It does not allow null values.
It is a sorted set.

6) HashMap class:
It is an implementation of hashing technique with array representation.
It is a two dimensional collection class and it maintains data as a key/value pairs
because it implements Map interface
It does not allow duplicate keys(Values may be duplicated)
It supports all types of keys and all types of values.
It allows null keys & null values.
It is an unordered map.

7) LinkedHashMap class:
It is an implementation of hashing technique with linked representation.
It is a two dimensional collection class and it maintains data as a key/value pairs
because it implements Map interface
It does not allow duplicate keys(Values may be duplicated)
It supports all types of keys and all types of values.
It allows null keys & null values. It is an ordered map.

[email protected] Naresh i Technologies


Java By Venkatesh Mansani Naresh i Technologies

8) TreeMap class:
It is an implementation of binary search technique with linked representation.
It is a two dimensional collection class and it maintains data as a key/value pairs
because it implements Map interface
It does not allow duplicate keys(Values may be duplicated)
It supports all types of keys and all types of values.
It does not allow null keys(null values allowed)
It is a sorted map.

Differences between Set and Map implementation classes:


HashSet HashMap
LinkedHashSet LinkedHashMap
TreeSet TreeMap
_______________________ _________________________
1) These are one dimensional 1) These are two dimensional
collections. collections.
2) These collections contain 2) These collections contain
elements. Key/value pairs.
3) These collections do not 3) These collections do not
allow duplicate elements. allow duplicate keys.
4) These are index based 4) These are key based
collections. collections.

9) PriorityQueue class:
It is an array representation of queue implementation class.
It allows insertion at rear end and deletion at front end only.

[email protected] Naresh i Technologies


Java By Venkatesh Mansani Naresh i Technologies

It allows duplicate elements.


It supports all types of data.
It does not allow null values.

10) ArrayDeque class:


It is an array representation of Deque implementation class.
It allows both insertion & deletion at both the ends(front end & rear end)
It allows duplicate elements.
It supports all types of data.
It does not allow null values.

More Utility Collections:


1) Iterator (interface)
2) ListIterator (interface)
3) Arrays (class)
4) Collections (class)
5) Scanner (class)

Iterator interface:
It is used to iterate elements of a collection.

ListIterator interface:
It is also used to iterate elements of a collection.

Differences between Iterator & ListIterator


Iterator ListIterator
1) It is used to iterate elements 1) It is used to iterate
of any collection. elements of ArrayList
& LinkedList only.

[email protected] Naresh i Technologies


Java By Venkatesh Mansani Naresh i Technologies

2) It supports only forward 2) It supports both


direction to iterate elements. forward and backward directions to iterate
elements.
3) It allows only remove 3) It allows add, remove & set
operation while iterating operations while iterating elements.
elements.

Iterator Example:
import java.util.*;
class Demo
{
public static void main(String args[])
{
ArrayList<Integer> al=new ArrayList<>();
al.add(83);
al.add(38);
al.add(81);
al.add(78);
al.add(73);
System.out.println(al);
Iterator<Integer> i=al.iterator();
while(i.hasNext())
{
int x=i.next();
System.out.println(x+5);
}

[email protected] Naresh i Technologies


Java By Venkatesh Mansani Naresh i Technologies

}
}

Scanner:
It is used accept the data from keyboard

Example:
import java.util.*;
class Demo
{
public static void main(String args[])
{
System.out.print(“Enter any number: “);
Scanner s=new Scanner(System.in);
int x=s.nextInt();
System.out.println(x);
}
}

Legacy Collections:
JDK 1.0 & 1.1 versions collection classes & interfaces are called legacy collections.
1) Enumeration(interface)
2) Vector(class)
3) StringTokenizer(class)
4) Hashtable(class)
5) Random(class)
6) Stack(class)

[email protected] Naresh i Technologies


Java By Venkatesh Mansani Naresh i Technologies

7) Date(class)

1) Enumeration interface:
It is used to iterate elements of a collection. It is similar to Iterator interface.
Differences between Enumeration & Iterator:
Enumeration Iterator
1) It is a legacy collection 1) It is a collections framework
interface. interface.
2) It does not allow other 2) It allows remove operations
operations while iterating while iterating elements.
elements.

2) Vector class:
It is an implementation of linear list data structure. It is similar to ArrayList class.

Differences between Vector & ArrayList


Vector ArrayList
1) It is a legacy collection 1) It is a collections framework class.
class.
2) Methods of Vector class 2) Methods of ArrayList class are synchronized.
are not synchronized.

3) StringTokenizer class:
It allows an application to break a string into tokens.
Example:
"Welcome to Sathya Technologies" is a one string and it has 4 tokens(words).

[email protected] Naresh i Technologies


Java By Venkatesh Mansani Naresh i Technologies

4) Hashtable class:
It is a two dimensional collection class and it maintains data as a key/value pairs.
It is an implementation of hashing technique with array representation. It is
similar to HashMap class.

Differences between Hashtable & HashMap


Hashtable HashMap
1) It is a legacy collection 1) It is a collections framework
class. class.
2) Methods of Hashtable are 2) Methods of HashMap are not
synchronized. synchronized.
3) Hashtable does not allow 3) HashMap allows one null key
null keys & null values. and many null values.

5) Random class:
It is used to get random integers, floating point numbers & boolean values.

Example:
import java.util.*;
class Demo
{
public static void main(String args[])
{
Random r=new Random();
for(int i=1;i<=10;i++)
{
System.out.println(r.nextInt(1000));

[email protected] Naresh i Technologies


Java By Venkatesh Mansani Naresh i Technologies

}
}
}

6) Stack class:
It is called as Last In First Out (LIFO) list.

Example:
import java.util.*;
class Demo
{
public static void main(String args[])
{
Stack<Integer> s=new Stack<>();
s.push(10);
s.push(83);
s.push(75);
s.push(87);
s.push(73);
System.out.println(s);
System.out.println(s.pop());
System.out.println(s);
}
}

[email protected] Naresh i Technologies


Java By Venkatesh Mansani Naresh i Technologies

Differences between Stack & Queue:


Stack Queue
1) It is called as Last In First 1) It is called as First In First Out(LIFO) list.
Out(FIFO) list.
2) It is a Stack data structure. 2) It is a Queue data structure.
3) It allows both insertion 3) It allows insertion at rear end
and deletion at top end only. and deletion at front end.
4) It is a legacy collection 4) It is a collections framework class.
interface.

7) Date class:
It is used to get the system date & time.

Example:
import java.util.*;
class Demo
{
public static void main(String args[])
{
Date d=new Date();
int x=d.getHours();
int y=d.getMinutes();
int z=d.getSeconds();
System.out.println(x+”:”+y+”:”+z);
}
}

[email protected] Naresh i Technologies

You might also like