java collections

57
7/21/2019 Java Collections http://slidepdf.com/reader/full/java-collections-56da1bbd35524 1/57

Upload: john

Post on 05-Mar-2016

218 views

Category:

Documents


0 download

DESCRIPTION

The documents is all about Java Collections

TRANSCRIPT

Page 1: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 1/57

Page 2: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 2/57

Page 3: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 3/57

Page 4: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 4/57

Page 5: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 5/57

Page 6: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 6/57

Page 7: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 7/57

The entire listing of interfaces, implementation classes and algorithm classes are

present in java.util package except Comparable interface.

Map does not extend java.util.Collection.

All implementation classes marked as (Legacy) contains synchronized methods. It does

not support concurrent access.

Page 8: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 8/57

Refer: http://download.oracle.com/javase/1.4.2/docs/api/java/util/Collection.html for

description of other methods of Collection interface.

Page 9: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 9/57

Page 10: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 10/57

Performance of sorting and searching operations with collections of size n is measured

using Big-O notation. The notation describes the complexity of the algorithm in

relation to the maximum time in which an algorithm operates, for large values of n. For

instance, if you iterate through an entire collection to find an element, the Big-O

notation is referred to as O(n), meaning that as n increases, time to find an element in

a collection of size n increases linearly. This demonstrates that Big-O notation assumes

worst case performance. It is always possible that performance is quicker.

For more information on Big O notation refer:

http://en.wikipedia.org/wiki/Big_O_notation

Page 11: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 11/57

LinkedList also implements Deque<E>, Queue<E> from Java version 1.6 onwards.

The Queue and Deque interface provides first-in-first-out queue operations for add,

poll.

Page 12: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 12/57

For information on other methods Refer:

http://download.oracle.com/javase/6/docs/api/java/util/LinkedList.html

Page 13: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 13/57

Page 14: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 14/57

Page 15: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 15/57

With Generics the collections becomes type-safe.

If the code compiles the programmer will not get any class-cast exception at runtime.

Page 16: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 16/57

Note: Try writing a generic code that can work on many types of containers like Set

and List.

Page 17: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 17/57

Page 18: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 18/57

Page 19: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 19/57

Page 20: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 20/57

Page 21: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 21/57

The code of compareTo() is written assuming that the employeeId is unique.

If the comparison is based on fields which are not unique: if the difference of two

values result in the value 0, then return the difference of 

other fields.Example:

public int compareTo(Employee employee) {

int difference =

this.getFirstName().compareTo(employee.getFirstName());

if( difference == 0 ) {

difference =

Double.compare(this.getSalary(), employee.getSalary());

}

}

return difference;

Page 22: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 22/57

Page 23: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 23/57

Page 24: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 24/57

Inner classes can access all the members of outer class even if the outer class

members are private.

Inner classes can be static or non-static.

Page 25: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 25/57

Page 26: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 26/57

Page 27: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 27/57

Whenever hashCode() returns same INTEGER, equals() method is used to check

objects equality.

Page 28: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 28/57

Page 29: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 29/57

Refer:http://www.cs.auckland.ac.nz/~jmor159/PLDS210/red_black.html for

information on RedBlack tree.

Page 30: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 30/57

Page 31: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 31/57

Page 32: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 32/57

Page 33: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 33/57

Page 34: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 34/57

Read more about Load Factor and Rehashing :http://goo.gl/uumsm

Page 35: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 35/57

Page 36: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 36/57

To specify the load factor create instance as new HashMap(0.75f); i.e. if 75% of

buckets are filled bucket size increases and rehashing takes place.

Page 37: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 37/57

Object get(Object key) method of Map returns “null” if key is not found.

Page 38: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 38/57

Page 39: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 39/57

Refer : 1) http://javahowto.blogspot.com/2006/06/4-ways-to-traverse-map.html

2) http://stackoverflow.com/questions/3870064/performance-considerations-

for-keyset-and-entryset-of-map

3) http://ulibgcj.sourceforge.net/javadoc/java/util/HashMap.html

Page 40: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 40/57

Page 41: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 41/57

Page 42: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 42/57

Page 43: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 43/57

Page 44: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 44/57

Page 45: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 45/57

Page 46: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 46/57

Refer: http://goo.gl/Y3Ybq for information on Enum type.

Page 47: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 47/57

Page 48: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 48/57

Page 49: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 49/57

Similarly complete this flow chart for choosing a Map.

Page 50: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 50/57

Page 51: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 51/57

Page 52: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 52/57

Page 53: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 53/57

Page 54: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 54/57

Page 55: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 55/57

Page 56: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 56/57

Page 57: Java Collections

7/21/2019 Java Collections

http://slidepdf.com/reader/full/java-collections-56da1bbd35524 57/57