java collections

Post on 05-Mar-2016

218 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

DESCRIPTION

The documents is all about Java Collections

TRANSCRIPT

7/21/2019 Java Collections

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

7/21/2019 Java Collections

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

7/21/2019 Java Collections

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

7/21/2019 Java Collections

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

7/21/2019 Java Collections

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

7/21/2019 Java Collections

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

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.

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.

7/21/2019 Java Collections

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

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

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.

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

7/21/2019 Java Collections

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

7/21/2019 Java Collections

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

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.

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.

7/21/2019 Java Collections

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

7/21/2019 Java Collections

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

7/21/2019 Java Collections

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

7/21/2019 Java Collections

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

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;

7/21/2019 Java Collections

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

7/21/2019 Java Collections

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

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.

7/21/2019 Java Collections

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

7/21/2019 Java Collections

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

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.

7/21/2019 Java Collections

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

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.

7/21/2019 Java Collections

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

7/21/2019 Java Collections

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

7/21/2019 Java Collections

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

7/21/2019 Java Collections

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

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

7/21/2019 Java Collections

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

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.

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.

7/21/2019 Java Collections

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

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

7/21/2019 Java Collections

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

7/21/2019 Java Collections

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

7/21/2019 Java Collections

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

7/21/2019 Java Collections

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

7/21/2019 Java Collections

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

7/21/2019 Java Collections

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

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.

7/21/2019 Java Collections

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

7/21/2019 Java Collections

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

7/21/2019 Java Collections

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

Similarly complete this flow chart for choosing a Map.

7/21/2019 Java Collections

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

7/21/2019 Java Collections

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

7/21/2019 Java Collections

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

7/21/2019 Java Collections

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

7/21/2019 Java Collections

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

7/21/2019 Java Collections

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

7/21/2019 Java Collections

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

7/21/2019 Java Collections

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

top related