Professional Documents
Culture Documents
and
Generics
java.util package
By Waqas
Interfaces
Abstract data types to manipulate collections
independent of implementation
Collection, Set, List, Map etc.
By Waqas
details.
e.g.
Implementations
Concrete
classes
which
are
implementing
Algorithms
Methods to perform operations on collections
By Waqas
such as sorting, searching,
iterations.
Collection Framework
Interfaces
By Waqas
Collection
Set
SortedSet
Map
List
SortedMap
Collection Framework
Interfaces
By Waqas
Collection Interface
A Collection interface is the super interface in the
collection interfaces hierarchy. A collection
represents a group of objects, known as its elements.
Java does not provide any direct implementation of
this interface.
It provides implementations of more specific
subinterfaces like Set and List.
By Waqas
Set Interface
A Set is a Collection that does not contain duplicate
elements, so its a collection of unique elements. If
you add duplicate element in Set then add( ) method
simply ignore the element and return false.
The elements in Set are not in order.
The set interface does not define any additional
methods, it inherits all the methods of Collection
interface.
By Waqas
List Interface
The List interface also extends Collection interface
and declares the behavior of a collection that stores a
sequence of objects in order.
Elements can be inserted or retrieved by their
position in the list, using a zero based index.
List can accept duplicate values.
By Waqas
10
SortedSet Interface
The SortedSet interface extends Set and declares the
behavior of a set in which objects are sorted in either
their natural order or the order you specified in your
custom object.
SortedSet does not accept duplicate elements.
By Waqas
11
Map Interface
A Map is an object that maps keys to values.
A map cannot contain duplicate keys but can contain
duplicate values.
Each key can map to at most one value.
We can retrieve value by using its key.
Elements are not orderedBy Waqas
12
SortedMap Interface
The SortedMap interface extends Map and declares
the behavior of a map in which objects are sorted by
their keys in their natural order or custom order.
By Waqas
13
Duplicates
Order
Sorting
Set
Not Allowed
No Order
No Sorting
List
Allowed
Order by Index
No Sorting
Map
No Order
No Sorting
Sorted Set
Not Allowed
By Natural Order
By Natural Order
Sorted Map
Not Allowed
By Natural Order
By Natural Order
By Waqas
14
Collection Framework
Implementations
By Waqas
15
Set
SortedSet
HashSet
TreeSet
List
ArrayList
LinkedList
Vector
Map
HashMap
SortedMap
Collection Framework
Implementations
TreeMap
By Waqas
16
ArrayList Class
ArrayList class implements the List interface.
ArrayList is a dynamic array that can grow and shrink
automatically as needed.
Insertions and deletions are linear thats why these
operations are slow in ArrayList
Searching is fast in ArrayList because Java performs
search randomly.
By Waqas
17
LinkedList Class
LinkedList class implements the List interface.
Every element in LinkedList contains the data item
and the pointer to the next node.
Insertions and deletions are fast because they are
linear in time.
Searching is slow as java search LinkedList
sequentially not randomly.
By Waqas
18
HashSet Class
HastSet provides an implementation of Set
interface.
Objects are not stored in order thats why
searching objects is very fast.
19
TreeSet Class
TreeSet provides an implementation of SortedSet
interface.
Objects are stored in sorted, ascending order.
Access and retrieval times is not as fast as HashSet.
TreeSet is an excellent choice when you want to
store large amounts of sorted data items.
By Waqas
20
HashMap Class
HashMap provides an implementation of Map
interface.
Objects are stored as key-value pairs.
null objects are supported by the HashMap.
Objects are not stored in order.
By Waqas
21
TreeMap Class
TreeMap class implements SortedMap interface.
It provides an efficient means of storing key-value
pairs in sorted order based on their keys.
22
Comparable Interface
Many java collection framework classes such as
TreeMap, TreeSet perform automatic sorting of
objects when they added in the collection.
For sorting objects they must be comparable. Java
provides an interface to make two objects
comparable.
Custom classes should implement comparable
interface to provide logic for class specific sorting
By Waqas
23
Comparable Interface
class Student implements Comparable
{
int id;
public int compareTo(Object obj)
{
Student s2 = (Student) obj;
Integer st1 = new Integer(this.id);
Integer st2 = new Integer(s2.id);
return st1.compareTo(st2);
By Waqas
24
Arrays Class
Arrays class contains
manipulating arrays.
various
methods
for
Comparator Interface
Comparator interface is used to create objects
which can be passed to Arrays.sort or Collection.sort
methods or collections such as TreeMap and
TreeSet.
They are used to sort custom objects in collections.
They are not needed for arrays and collections of
primitive data types and for objects that have a
natural sorting order such as String, Integer.
By Waqas
26
Comparator Interface
class StudentComparator implements
Comparator
{
public int compare(Object obj1, Object
obj2)
{
Student s1 = (Student) obj1;
Student s2 = (Student) obj2;
Integer st1 = new Integer(s1.id);
Integer st2 = new Integer(s2.id);
By Waqas
27
Comparator Interface
Student students[] = new Student[5];
students[0]
students[1]
students[2]
students[3]
students[4]
=
=
=
=
=
new
new
new
new
new
Student(5,
Student(2,
Student(1,
Student(4,
Student(3,
By Waqasnew
Arrays.sort(students,
"Simon");
"James");
"Peter");
"David");
"John");
28
Collections Class
Collections class contains various methods for
manipulating collections.
sort( List );
binarySearch( List, Object );
min(List);
max(List);
replaceAll(List, Object, Object);
reverse(List);
shuffle(List);
swap(List, int, int);
By Waqas
29
Java Generics
By Waqas
30
// Compiler Error
31
By Waqas
32
Java Generics
Generics in Java allow us to create collections with
a strong type.
This means that if we are creating a collection to
store Strings we will be forced to store and retrieve
only Strings at compile time and runtime.
Overall result of using generics in java collections is
improved reliability, readability and performance.
By Waqas
33
Generic ArrayList
ArrayList<E> list = new ArrayList<E>();
ArrayList<String> list = new ArrayList<String>();
list.add(Simon);
list.add(Peter);
list.add(new Integer(2));
// Compiler Error
String s1 = list.get(0);
String s2 = list.get(1);
Integer I = list.get(0);
By Waqas
// Compiler Error
34
Generic TreeSet
TreeSet<E> set = new TreeSet<E>();
By Waqas
35
Generic TreeMap
TreeMap<K,V> map = new TreeMap<K,V>();
By Waqas
36
Generic Iterator
Iterator<E> it = set.iterator();
>();TreeSet<String> set = new TreeSet<String
set.add(Simon);
set.add(Peter);
Iterator<String> it = set.iterator();
String s;
while(it.hasNext())
{
s = it.next();
}
By Waqas
37