Date post: | 03-Apr-2018 |
Category: |
Documents |
Upload: | sasirekha-perumalvijayan |
View: | 214 times |
Download: | 0 times |
of 81
7/29/2019 java-util-120826225201-phpapp01
1/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
java.util and
CollectionFramework
7/29/2019 java-util-120826225201-phpapp01
2/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
This presentation is for teaching purpose only.
This presentation may contain some material
from books / api documentation / internet.
No intention of breaking any rights or what so
ever.
7/29/2019 java-util-120826225201-phpapp01
3/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
Date and Time Related Classes
Date
TimeZone
Calendar GregorianCalendar
Epoch : 1st Jan 1970 0:0:0.0 GMT 1st January 1970 5:30:0.0 IST
7/29/2019 java-util-120826225201-phpapp01
4/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
Simple Program
????????? toString()
7/29/2019 java-util-120826225201-phpapp01
5/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
Constructor
7/29/2019 java-util-120826225201-phpapp01
6/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
Comparing Date
7/29/2019 java-util-120826225201-phpapp01
7/81Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
Calendar
Abstract Class
No public constructor
day, month, year, hour, minutes , seconds, era Many int constants
Many methods
7/29/2019 java-util-120826225201-phpapp01
8/81Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
7/29/2019 java-util-120826225201-phpapp01
9/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
Some Methods
7/29/2019 java-util-120826225201-phpapp01
10/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
TimeZone
Class
Maintains offset value
ID for instances
public static TimeZone getDefault()
public static void setDefault(TimeZone zone)
7/29/2019 java-util-120826225201-phpapp01
11/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
7/29/2019 java-util-120826225201-phpapp01
12/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
public String getId()
public String getDisplayName()
public int getOffset(long date) public int getRawOffset
public void setRawOffset(int offmilliseconds)
public static TimeZone getTimeZone(String ID)
7/29/2019 java-util-120826225201-phpapp01
13/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
7/29/2019 java-util-120826225201-phpapp01
14/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
7/29/2019 java-util-120826225201-phpapp01
15/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
7/29/2019 java-util-120826225201-phpapp01
16/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
7/29/2019 java-util-120826225201-phpapp01
17/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
GregorianCalendar
Concrete class of Calendar
Two fields AD and BC for era
7 constructors GreogrianCalendar gc = new
GregorianCalendar();
Current date and time in default locale and timezone
7/29/2019 java-util-120826225201-phpapp01
18/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
GregorianCalendar
Ff
Year = Number of year elapsed from 1900Month = 0 indicating Jan
7/29/2019 java-util-120826225201-phpapp01
19/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
GregorianCalendar
fd
boolean isLeapYear(int year)
Test for leap year
7/29/2019 java-util-120826225201-phpapp01
20/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
DateFormat
7/29/2019 java-util-120826225201-phpapp01
21/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
7/29/2019 java-util-120826225201-phpapp01
22/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
7/29/2019 java-util-120826225201-phpapp01
23/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
Array Class
Contains various methods for manipulatingarrays - mostly static methods
Contains a static factory that allows arrays to
be viewed as lists.
fill, equals, sort, binarysearch, toString
7/29/2019 java-util-120826225201-phpapp01
24/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
fill method
public static void fill(datatype[] array, datatype
value)
public static void fill(datatype[] array, int
fromindex , int toindex, datatype value)
datatype = Primitive Datatype + Object
7/29/2019 java-util-120826225201-phpapp01
25/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
Program
7/29/2019 java-util-120826225201-phpapp01
26/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
Output
7/29/2019 java-util-120826225201-phpapp01
27/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
fill()
7/29/2019 java-util-120826225201-phpapp01
28/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
fill()
7/29/2019 java-util-120826225201-phpapp01
29/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
equals()
Return Boolean
Take two arguments
public static boolean equals( datatype [] a,datatype[] b)
Compare each element and size of array
7/29/2019 java-util-120826225201-phpapp01
30/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
equals()
7/29/2019 java-util-120826225201-phpapp01
31/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
sort()
Sort the array
Ascending Order
7/29/2019 java-util-120826225201-phpapp01
32/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
sort()
7/29/2019 java-util-120826225201-phpapp01
33/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
binarySearch()
Uses binary search method to find the index
of element
Prerequisites : Sorted Array
7/29/2019 java-util-120826225201-phpapp01
34/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
binarySearch()
7/29/2019 java-util-120826225201-phpapp01
35/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
Collection Framework
Standardizing and having interoperability
between various data structures
Introduced in Java 1.2
Relies on Standard interfaces
7/29/2019 java-util-120826225201-phpapp01
36/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
Collection Framework
Eight generics interfaces type
Determined the common methods for all
collection class
Operations
Basic Operations
Array Operations
Bulk Operations
7/29/2019 java-util-120826225201-phpapp01
37/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
7/29/2019 java-util-120826225201-phpapp01
38/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
Basic Operations
7/29/2019 java-util-120826225201-phpapp01
39/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
Basic Operations
7/29/2019 java-util-120826225201-phpapp01
40/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
Array Operations
7/29/2019 java-util-120826225201-phpapp01
41/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
Bulk Operation
7/29/2019 java-util-120826225201-phpapp01
42/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
Iterator
Lets you cycle through collection
7/29/2019 java-util-120826225201-phpapp01
43/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
Example
7/29/2019 java-util-120826225201-phpapp01
44/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
Set Interface
Defines a set
Collection of unique element [ no duplication]
Extends the Collection
All the methods
HashSet and TreeSet are concrete classes
AbstractSet : base abstract class
7/29/2019 java-util-120826225201-phpapp01
45/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
List Interface
Extends Collection
Insertion, updating, all are index based
Index starts from 0
It can store duplicates value
It adds its own methods
7/29/2019 java-util-120826225201-phpapp01
46/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
Methods
7/29/2019 java-util-120826225201-phpapp01
47/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
Methods
7/29/2019 java-util-120826225201-phpapp01
48/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
Methods
7/29/2019 java-util-120826225201-phpapp01
49/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
List Iterator
public interface ListIterator extends
Iterator
A ListIterator has no current element;
its cursor position always lies between the
element that would be returned by a call
toprevious() and the element that would be
returned by a call to next(). An iterator for a list of length n has
n+1 possible cursor positions,
http://e/JavaSoft/jdk-7-ea-apidocs/docs/api/java/util/Iterator.htmlhttp://e/JavaSoft/jdk-7-ea-apidocs/docs/api/java/util/Iterator.html7/29/2019 java-util-120826225201-phpapp01
50/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
List Iterator
7/29/2019 java-util-120826225201-phpapp01
51/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
7/29/2019 java-util-120826225201-phpapp01
52/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
7/29/2019 java-util-120826225201-phpapp01
53/81
7/29/2019 java-util-120826225201-phpapp01
54/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
Vector Class
7/29/2019 java-util-120826225201-phpapp01
55/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
Vector Class
Dynamic Array
The size of a Vector can grow or shrink as needed
to accommodate adding and removing items after
the Vector has been created
Synchronized
Each vector tries to optimize storagemanagement by maintaining a capacity and
a capacityIncrement
7/29/2019 java-util-120826225201-phpapp01
56/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
Vector Class
Modified from Java 1.2 [ IS IT SAME AS JAVA
2]
To implement List and become part of
Collection Framework
7/29/2019 java-util-120826225201-phpapp01
57/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
Vector Class Constructors
The increment specifies the number ofelements to allocate each time that a vector is
resized upward.
Initial Size 10
7/29/2019 java-util-120826225201-phpapp01
58/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
Vector Class
Starts with initial capacity
Allocates more space for new objects once the
capacity is reached
It allocates more space that required so
allocation is done minimal time
Double size if nothing is specified
What if incr size = 0 [ Default Value]
7/29/2019 java-util-120826225201-phpapp01
59/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
Vector Class
7/29/2019 java-util-120826225201-phpapp01
60/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
Vector Class
7/29/2019 java-util-120826225201-phpapp01
61/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
Vector Class
7/29/2019 java-util-120826225201-phpapp01
62/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
Vector Class
7/29/2019 java-util-120826225201-phpapp01
63/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
Vector Class
7/29/2019 java-util-120826225201-phpapp01
64/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
Vector Class
7/29/2019 java-util-120826225201-phpapp01
65/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
Vector Class
7/29/2019 java-util-120826225201-phpapp01
66/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
7/29/2019 java-util-120826225201-phpapp01
67/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
Now Lets add elements
addElement(Object o)
Size = ? Capacity = ?
7/29/2019 java-util-120826225201-phpapp01
68/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
Size = 4 Capacity = 5
WHY
Size = ? Capacity = ?
5,5 5,8
Any Other Option
7/29/2019 java-util-120826225201-phpapp01
69/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
7/29/2019 java-util-120826225201-phpapp01
70/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
7/29/2019 java-util-120826225201-phpapp01
71/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
7/29/2019 java-util-120826225201-phpapp01
72/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
Solution : Generics
Specifies the data type
Vector v = new Vector ();
Vector v = new
Vector ();
7/29/2019 java-util-120826225201-phpapp01
73/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
Printing Vector
7/29/2019 java-util-120826225201-phpapp01
74/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
One Example
7/29/2019 java-util-120826225201-phpapp01
75/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
Lets Create Vector
Size = ?? Capacity = ?? DEFAULT = 10
7/29/2019 java-util-120826225201-phpapp01
76/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
Will This Work !!!
7/29/2019 java-util-120826225201-phpapp01
77/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
How to Print area ?
7/29/2019 java-util-120826225201-phpapp01
78/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
Stack
7/29/2019 java-util-120826225201-phpapp01
79/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
ArrayList
7/29/2019 java-util-120826225201-phpapp01
80/81
Prof. Ashish BhatiaAvailable at : http://www.asbspace.in/ppt
ArrayList
Resizable-array implementation of
the List interface.
Implements all optional list operations, and
permits all elements, including null.
This class is roughly equivalent to Vector,
except that it is unsynchronized.
f
7/29/2019 java-util-120826225201-phpapp01
81/81
Map Interface
Continued to Next Presentation