Professional Documents
Culture Documents
Objectives
NIIT
Lesson 1A / Slide 1 of 30
Java Packages
Generic Types:
A geneic type represents a class or an interface that a type-safe. It means it can
act on any data type.
Generic types are designed to act upon only objects not with primitive data
types.
Since it acts upon any datatype, in the place of the datatype, we use a generic
parameter like <T> or <GT>
Generic class:
represents a class that is typesafe.
class MyClass<T>{
T obj;
}
Here T represents any datatype. The datatype is specified by the
programmer at the time of creating objects of MyClass as
MyClass<String> obj=new MyClass<String>();
NIIT
Lesson 1A / Slide 2 of 30
Java Packages
Generic Types:
Generic methods:
represents a method that is typesafe.
<T> void display(){
// method code;
}
NIIT
Lesson 1A / Slide 3 of 30
Java Packages
The Collection Framework:
It is possible to store group of objects into an array.
for ex:- Student arr[]=new Student[50];
And we can retrieve them again but we cant do certain operations very
effectively such as inserting and deleting objects, etc from an array.
The alternative way is using an object, instead of an array, to store a group of
objects. Such an object is called Collection Object or Container Object
A Collection object stores references of other objects not store the copies of
other objects.
java.util
collection classes
collection objects
NIIT
Lesson 1A / Slide 4 of 30
Java Packages
The Collection Interface
A collection is an object that contains a group of objects within it.
These objects are called the elements of the collection. The elements
of a collection descend from a common parent type.
Collections have an advantage over arrays that collections can grow to
any size unlike arrays.
The following constructors are used for the collection interface:
CollectionName(): Creates an empty collection. A void
NIIT
Lesson 1A / Slide 5 of 30
Java Packages
Inteface type
Implementation classes
Set <T>
HashSet <T>
LinkedHashSet <T>
List <T>
Queue <T>
LinkedList <T>
Map <K,V>
HashMap <K,V>
HashTable <K,V>
NIIT
Lesson 1A / Slide 6 of 30
Java Packages
Exploring java.util Package
Class
Description
StringToken
izer
Arrays
Date
Calendar
GregorianCa
lendar
NIIT
Lesson 1A / Slide 7 of 30
Java Packages
Sets: A Set represents a group of elements arranged just like an array. A Set will
not allow duplicate elements.
Lists: Lists are like Sets. But lists allow duplicate values to be stored.
NIIT
Lesson 1A / Slide 8 of 30
Java Packages
for each: to repeatedly executes a group of statements
NIIT
Methods
Description
boolean hasNext( )
element next( )
void remove()
Lesson 1A / Slide 9 of 30
Java Packages
ListIterator: it contains Methods to retrieve elements both in forward and
reverse directions.
Methods
Description
boolean hasNext( )
element previous( )
void remove()
NIIT
Methods
Description
boolean hasMoreElements( )
element nexElement( )
Lesson 1A / Slide 10 of 30
Java Packages
HashSet:
HashSet represents a set of elements. It doesnt guarantee the order of
elements.
Constructors:
HashSet()
HashSet(int capacity);
HashSet Methods:
NIIT
boolean add( )
boolean remove()
void clear()
boolean contains( )
boolean isEmpty()
int size()
Lesson 1A / Slide 11 of 30
Java Packages
Stack Class:
A Stack represents a group of elements in LIFO ( Last in First Out) order.
Constructors:
Stack()
Ex:- Stack <Integer> stk= new Stack<Integer>();
Stack Methods:
boolean empty()
element peek( )
element pop( )
int search(element)
NIIT
element push(element)
Lesson 1A / Slide 12 of 30
Java Packages
LinkedList Class:
A LinkedList represents a group of elements in
Constructors:
LinkedList()
Ex:- LinkedList <String> llist= new LinkedList<String>();
LinkedList Methods:
boolean add(obj)
void add(pos,obj)
void addFirst(obj)
void addLast(obj)
obj removeFirst()
obj removeLast()
void clear()
obj getFirst()
obj getFirst()
int indexOf(obj)
int lastIndexOf(obj)
Object[] toArray()
NIIT
Lesson 1A / Slide 13 of 30
Java Packages
ArrayList Class:
A ArrayList is like an array, which can grow in memory dynamically.
ArrayList is not Synchronized
Constructors:
ArrayList()
Ex:- ArrayList <String> arlst= new ArrayList<String>(); // cap - 10
ArrayList Methods:
boolean add(obj)
void add(pos,obj)
boolean remove(obj)
void clear()
int size()
boolean contains(obj)
int indexOf(obj)
NIIT
Lesson 1A / Slide 14 of 30
Java Packages
Vector Class:
A Vector also stores elements (objects) similar to ArrayList, but vector is
synchronized.
Constructors:
Vector()
Ex:- Vector<String> v= new Vector<String>(); // default capacity - 10
Vector Methods:
boolean add(obj)
void add(pos,obj)
boolean remove(obj)
void clear()
voidremoveAllElements( )
String toString()
boolean removeElement(obj)
int size()
boolean contains(obj)
int indexOf(obj)
NIIT
Lesson 1A / Slide 15 of 30
Java Packages
HashMap / HasTable Class:
HashMap / HashTable is a Collection that stores elements in the form of
key-value pairs.
Keys should be unique.
HashMap is not Synchronized, where as HashTable is Synchronized.
Constructors:
HashMap<K,V>()
Ex:- HashMap<String, Integer> hm= new HashMap<String,Integer>();
HashMap Methods:
value put(key,value)
boolean isEmpty()
void clear()
int size()
NIIT
Lesson 1A / Slide 16 of 30
Java Packages
Arrays Class:
Arrays class provides methods to perform certain operations on any one
dimensional array.
NIIT
Lesson 1A / Slide 17 of 30
Java Packages
StreamTokenizer Class
The
one
The
The
and
StringTokenizer Methods:
int countTokens()
NIIT
boolean hasMoreTokens()
String nextToken()
Lesson 1A / Slide 18 of 30
Java Packages
StreamTokenizer Class
Methods
Description
NIIT
Collaborate
Lesson 2C / Slide 19 of 22
Java Packages
StreamTokenizer Class(Contd.)
Methods
Description
int nextToken()
NIIT
Collaborate
Lesson 2C / Slide 20 of 22
Java Packages
StreamTokenizer Class(Contd.)
The StreamTokenizer class has the ttype as an instance variable that contains
the type of token read by the nextToken() method.
For a single character token the ttype variable contains the integer form of that
single character token. For a quoted string token the ttype variable can have
values, such as TT_EOF, TT_EOL, TT_WORD, and TT_NUMBER.
NIIT
Collaborate
Lesson 2C / Slide 21 of 22
Java Packages
NIIT
The Date class encapsulates the system date and time information.
The Java Development Kit (JDK) included various methods for converting dates
into year, month, day, hour, minute, or seconds.
Some of the constructors of the Date class are:
Date(): Initializes an object with the current date and time.
Date(long ms): Accepts an argument that represents the number of
milliseconds that have elapsed since January 1, 1970. This date is used as
the reference date for calculating the number of millseconds that have
passed till the current date.
Collaborate
Lesson 1C / Slide 22 of 20
Java Packages
Formatconsts:
DateFormat.FULL ; DateFormat.LONG ; DateFormat.MEDIUM ; DateFormat.SHORT
region
Locale.UK; Locale.US, Locate.CHINA, Locale.CANADA, etc.
NIIT
Collaborate
Lesson 1C / Slide 23 of 20
Java Packages
The following table lists the various methods of the Date class:
Method
Description
long getTime()
NIIT
Collaborate
Lesson 1C / Slide 24 of 20
Java Packages
Description
boolean equals(Object)
NIIT
Collaborate
Lesson 1C / Slide 25 of 20
Java Packages
The GregorianCalendar class derived from the abstract Calendar class that
defines the various methods that provide information about the interpretation of
time as year, month, day, hour, minute, or second.
GregorianCalendar
GregorianCalendar
GregorianCalendar
GregorianCalendar
calendar=new
calendar=new
calendar=new
calendar=new
GregorianCalendar();
GregorianCalendar(Locale.UK);
GregorianCalendar(int year,int month, int day);
GregorianCalendar(1985,Calendar.MARCH,10);
You can retrieve this as a Date object by calling the getTime() method
Date now=calendar.getTime();
NIIT
Collaborate
Lesson 1C / Slide 26 of 20
Java Packages
Setting the Date and Time
You have a setTime() method that you can pass a Date object to set a
GregorianCalendar object to the time specified by the Date object
You have several overloaded versions of the set() method of setting various
components of the Date and Time.
NIIT
Collaborate
Lesson 1C / Slide 27 of 20
Java Packages
Setting the Date and Time
set(int field, int value);
Field
Value
DATE or
DAY_OF_MONTH
MONTH
DAY_OF_WEEK
SUNDAY,MONDAY,etc or values 1 to 7
YEAR
DAY_OF_YEAR
HOUR
HOUR_OF_DAY
A valur from 0 to 23
NIIT
Collaborate
Lesson 1C / Slide 28 of 20
Java Packages
Setting the Date and Time
set(int field, int value);
Field
Value
MINUTE
A value from 0 to 59
SECOND
A value from 0 to 59
AM_PM
AM or PM or values of 0 and 1
ZONE_OFFSET
Indicating GMT
NIIT
Collaborate
Lesson 1C / Slide 29 of 20
Java Packages
Getting the Date and Time
You can get the information such as the day, the month, and the year from a
GregorianCalendar object by using the get() method
NIIT
Collaborate
Lesson 1C / Slide 30 of 20
Java Packages
The following table lists a few methods of defined by the Calendar class:
Method
Description
NIIT
Collaborate
Lesson 1C / Slide 31 of 20
Java Packages
Description
NIIT
Collaborate
Lesson 1C / Slide 32 of 20
Java Packages
Random Class
Constructors:
Random()
Random(long seed)
The default constructor will create an object that uses the current time from your
computer clock as the starting value for generating pseudo-random numbers
The other constructor accepts an argument of type long that will be used as seed.
NIIT
Collaborate
Lesson 1C / Slide 33 of 20
Java Packages
Random Operations
Method
Desciption
int nextInt()
float nextFloat()
double nextDouble()
long nextLong()
Collaborate
Lesson 1C / Slide 34 of 20
Java Packages
Summary
In this lesson, you learned:
NIIT
Lesson 1A / Slide 35 of 30
Java Packages
Summary (Contd.)
NIIT
Lesson 1A / Slide 36 of 30
Java Packages
Summary (Contd.)
NIIT
Lesson 1A / Slide 37 of 30