Collections Framework & Generics
Collections Framework & Generics
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.
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) 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.
3) Set interface:
It extends Collection interface and it maintains sets. It does not allow duplicate
elements.
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).
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.
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.
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>();
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.
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
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.
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.
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.
9) PriorityQueue class:
It is an array representation of queue implementation class.
It allows insertion at rear end and deletion at front end only.
Iterator interface:
It is used to iterate elements of a collection.
ListIterator interface:
It is also used to iterate elements of a collection.
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);
}
}
}
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)
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.
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).
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.
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));
}
}
}
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);
}
}
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);
}
}