+ All Categories
Home > Documents > Framework of Java Important Aima

Framework of Java Important Aima

Date post: 09-Jul-2016
Category:
Upload: dinesh
View: 12 times
Download: 0 times
Share this document with a friend
37
By Waqas 1 Java Collections and Generics java.util package
Transcript
Page 1: Framework of Java Important Aima

By Waqas 1

Java Collectionsand

Genericsjava.util package

Page 2: Framework of Java Important Aima

By Waqas 2

A Collection (sometimes called a Container) is simply an object that groups multiple objects into a single group.

The java collection framework standardizes the way in which groups of objects are handled by your programs by providing a unified architecture called “Collection Framework”

Java Collection Framework

Page 3: Framework of Java Important Aima

By Waqas 3

The Collection Framework is designed to meet several goals.

First, the framework provides high performance, fast and highly efficient way to work with groups of objects.

Second, the framework allows different types of collections to work in a similar manner with high degree of compatibility.

Page 4: Framework of Java Important Aima

By Waqas 4

The Collection Framework consists of three elements: Interfaces, Implementation and Algorithms.

Interfaces

Abstract data types to manipulate collections independent of implementation details. e.g. Collection, Set, List, Map etc.

Collection Framework Elements

Page 5: Framework of Java Important Aima

By Waqas 5

Implementations

Concrete classes which are implementing Collection interfaces, used as reusable data structures. e.g. HashMap, ArrayList, TreeSet, HashSet etc.

Algorithms

Methods to perform operations on collections such as sorting, searching, iterations.

Page 6: Framework of Java Important Aima

By Waqas 6

Collection FrameworkInterfaces

Page 7: Framework of Java Important Aima

By Waqas 7

Collection

Set List

SortedSet

Map

SortedMap

Collection Framework

Interfaces

Page 8: Framework of Java Important Aima

By Waqas 8

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.

Collection Interface

Page 9: Framework of Java Important Aima

By Waqas 9

A Set is a Collection that does not contain duplicate elements, so it’s 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.

Set Interface

Page 10: Framework of Java Important Aima

By Waqas 10

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.

List Interface

Page 11: Framework of Java Important Aima

By Waqas 11

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.

SortedSet Interface

Page 12: Framework of Java Important Aima

By Waqas 12

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 ordered

Map Interface

Page 13: Framework of Java Important Aima

By Waqas 13

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.

SortedMap elements can not contain duplicate keys.

SortedMap Interface

Page 14: Framework of Java Important Aima

By Waqas 14

Difference between Interfaces

By Natural OrderBy Natural OrderNot AllowedSorted Map

By Natural OrderBy Natural OrderNot AllowedSorted Set

No Order

Order by Index

No Order

Order

No Sorting

No Sorting

No Sorting

SortingDuplicatesInterface

AllowedList

Not Allowed for KeysMap

Not AllowedSet

Page 15: Framework of Java Important Aima

By Waqas 15

Collection FrameworkImplementations

Page 16: Framework of Java Important Aima

By Waqas 16

Set SortedSet

HashSet

List

Collection FrameworkImplementations

TreeSet ArrayList LinkedList

VectorMap SortedMap

HashMap TreeMap

Page 17: Framework of Java Important Aima

By Waqas 17

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 that’s why these operations are slow in ArrayList

Searching is fast in ArrayList because Java performs search randomly.

Page 18: Framework of Java Important Aima

By Waqas 18

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.

Page 19: Framework of Java Important Aima

By Waqas 19

HastSet provides an implementation of Set interface.

Objects are not stored in order that’s why searching objects is very fast.

HashSet does not support duplicate elements.

HashSet Class

Page 20: Framework of Java Important Aima

By Waqas 20

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.

TreeSet Class

Page 21: Framework of Java Important Aima

By Waqas 21

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.

HashMap Class

Page 22: Framework of Java Important Aima

By Waqas 22

TreeMap class implements SortedMap interface.

It provides an efficient means of storing key-value pairs in sorted order based on their keys.

As objects are sorted so random access or searching is little slower then HashMap.

TreeMap Class

Page 23: Framework of Java Important Aima

By Waqas 23

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

Comparable Interface

Page 24: Framework of Java Important Aima

By Waqas 24

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);}

}

Comparable Interface

Page 25: Framework of Java Important Aima

By Waqas 25

Arrays class contains various methods for manipulating arrays.

The two most common methods are sorting and searching.

To sort array pass the array in the sort method. This method sort all elements in their natural order.

To search elements inside array use binarySearch method.

Arrays Class

Page 26: Framework of Java Important Aima

By Waqas 26

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.

Comparator Interface

Page 27: Framework of Java Important Aima

By Waqas 27

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);

return st1.compareTo(st2);}

}

Comparator Interface

Page 28: Framework of Java Important Aima

By Waqas 28

Student students[] = new Student[5];

students[0] = new Student(5, "Simon");students[1] = new Student(2, "James");students[2] = new Student(1, "Peter");students[3] = new Student(4, "David");students[4] = new Student(3, "John");

Arrays.sort(students, new StudentComparator());

Comparator Interface

Page 29: Framework of Java Important Aima

By Waqas 29

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);

Collections Class

Page 30: Framework of Java Important Aima

By Waqas 30

Java Generics

Page 31: Framework of Java Important Aima

By Waqas 31

When we retrieve an element from a collection, we need to cast it to the right type otherwise compile time error occur.

ArrayList list = new ArrayList();String input = “London”; list.add(input);

String output = list.get(0); // Compiler Error

String output = (String) list.get(0); // Valid

Example of Generic Errors

Page 32: Framework of Java Important Aima

By Waqas 32

There is no way to ensure that we are casting to a correct type. Following code will compile but it will throw exception at runtime.

ArrayList list = new ArrayList();String input = “London”; list.add(input);

Integer output = list.get(0); // Runtime Error

Example of Generic Errors

Page 33: Framework of Java Important Aima

By Waqas 33

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.

Java Generics

Page 34: Framework of Java Important Aima

By Waqas 34

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); // Compiler Error

Generic ArrayList

Page 35: Framework of Java Important Aima

By Waqas 35

TreeSet<E> set = new TreeSet<E>();

TreeSet<Integer> set = new TreeSet<Integer;()<

set.add(new Integer(2)) ;set.add(new Integer(3));

set.add(“Simon”); // Compiler Error

Generic TreeSet

Page 36: Framework of Java Important Aima

By Waqas 36

TreeMap<K,V> map = new TreeMap<K,V>();

TreeMap<Integer, String> map = new TreeMap<Integer, String;()<

map.put(new Integer(2), “Simon”) ;map.put(new Integer(3), “Peter”);

map.put(“1”, “Simon”); // Compiler Errormap.put(“1”, new Integer(3)); // Compiler Error

Generic TreeMap

Page 37: Framework of Java Important Aima

By Waqas 37

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();}

Generic Iterator


Recommended