Util
Package
Ø This package
consists of some set of classes.
StringTokenizer
Ø This class
is present in java.util package.
Ø This class
is used to cut the string into pieces and each piece is known as token.
Ø To create
object for StringTokenizer class
StringTokenizer
st=new StringTokenizer(String,delimiter);
Ø By default
the delimiter is space.
Ø The
delimiter can be any special character.
Ø To count the
number of tokens present in the string we have execute countTokens () method.
st.countTokens
();
Ø To know
whether the token is available or not we have to use hasMoreTokens () method
and this method will return Boolean data type i.e. true or false.
st.hasMoreTokens
();
Ø To print the
next token we have to use nextToken () method.
st.nextToken
();
PROGRAM:- To cut the string into
pieces using StringTokenizer class.
import
java.util.*;
class
STdemo
{
public static void main(String args[])
{
String str="hello welcome to util
package";
StringTokenizer st=new
StringTokenizer(str);
System.out.println("Number of
tokens:"+st.countTokens());
while(st.hasMoreTokens())
{
System.out.println(st.nextToken());
}
}
}
File Name: -
STdemo.java
Commands:-
javac STdemo.java
java STdemo
OUTPUT:-
Number of tokens:
5
hello
welcome
to
util
package
PROGRAM:- To cut the string into
pieces using StringTokenizer class using delimiter
import
java.util.*;
class
STdemo1
{
public static void main(String args[])
{
String str="hello;welcome to:util
package";
StringTokenizer st=new
StringTokenizer(str,";:");
System.out.println("Number of
tokens:"+st.countTokens());
while (st.hasMoreTokens())
{
System.out.println(st.nextToken());
}
}
}
File Name: -
STdemo1.java
Commands:-
javac STdemo1.java
java STdemo1
OUTPUT:-
Number of tokens:
3
hello
welcome to
util package
Calendar
Ø This class
is present in java.util package.
Ø This class
is used to display date and time.
PROGRAM:- To display date and time.
import
java.util.*;
class
Caldemo
{
public static void main(String args[])
{
Calendar cl=Calendar.getInstance();
int d=cl.get(Calendar.DATE);
int m=cl.get(Calendar.MONTH);
int y=cl.get(Calendar.YEAR);
System.out.print("current
date:");
System.out.println(d+"-"+m+"-"+y);
int h=cl.get(Calendar.HOUR);
int min=cl.get(Calendar.MINUTE);
int s=cl.get(Calendar.SECOND);
System.out.print("current
time:");
System.out.println(h+":"+min+":"+s);
}
}
File Name: -
Caldemo.java
Commands:-
javac Caldemo.java
java Caldemo
OUTPUT:-
current date: 10-9-2013
current time: 7:30:45
Date
Ø This class
is present in java.util package.
Ø This class
is used to display date and time.
PROGRAM:- To display date and time.
import
java.util.*;
class
Datedemo
{
public static void main(String args[])
{
Date d=new Date ();
System.out.println(d);
}
}
File Name: -
Datedemo.java
Commands:-
javac Datedemo.java
java Datedemo
OUTPUT:-
Thu Nov 14 10:37:00 IST
2013
NOTE:-
Ø Whenever we
run the application the output will be displayed in the same format.
Ø To display
the output in different formats we can use DateFormat
class present in java.text package.
DateFormat
Ø This class
is present in java.text package.
Ø To display
only date we have to write the following syntax:
DateFormat
fmt=DateFormat.getDateInstance(constant,region);
Ø To display
only time we have to write the following syntax:
DateFormat
fmt1=DateFormat.getTimeInstance(constant,region);
Ø To display
date and time we have to write the following syntax:
DateFormat
fmt2=DateFormat.getDateTimeInstance(constant,constant,region);
Ø To display
the output we have to execute format () method.
Types of DateFormat Constants:
DateFormat.SHORT
DateFormat.LONG
DateFormat.FULL
DateFormat.MEDIUM
PROGRAM:- To display date and time
different formats.
import
java.util.*;
import
java.text.*;
class
Datedemo1
{
public static void main(String args[])
{
Date d=new Date();
DateFormat
fmt=DateFormat.getDateInstance(DateFormat.MEDIUM,Locale.UK);
DateFormat
fmt1=DateFormat.getDateTimeInstance(DateFormat.MEDIUM,DateFormat.SHORT,Locale.UK);
DateFormat fmt2=DateFormat.getTimeInstance(DateFormat.FULL,Locale.UK);
System.out.println(fmt.format(d));
System.out.println(fmt1.format(d));
System.out.println(fmt2.format(d));
}
}
File Name: -
Datedemo1.java
Commands:-
javac Datedemo1.java
java Datedemo1
OUTPUT:-
14-Nov-2013
14-Nov-2013 10:36
10:36:06 o’clock IST
COLLECTION
FRAME WORKER:-
Ø There are
some of classes and interfaces present in java.util package.
Classes:
1. ArrayList
2. Vector
3. Hashtable
4. HashMap
Intefaces:
1. Iterator: It
will get the elements one by one.
2. ListIterator:
It will get the elements in both directions i.e. Forward Direction and Reverse
Direction.
3. Enumeration:
In this when we supply a key it will give the value.
ArrayList
Ø The size of
the ArrayList increases dynamically at runtime.
Ø To create
object for ArrayList class
ArrayList arl=new
ArrayList (size);
Ø To add
elements to ArrayList we have to execute add
()method.
arl.add(“item”);
Ø To know the
number of elements present in ArrayList we have to execute size () method.
arl.size ();
PROGRAM:-
import
java.util.*;
class
Arraylistdemo
{
public static void main(String args[])
{
ArrayList arl=new ArrayList(2);
arl.add("C");
arl.add("C++");
arl.add("Java");
arl.add("Oracle");
System.out.println("Contents
are:"+arl);
System.out.println("Size
is:"+arl.size());
arl.remove(2);
System.out.println("Contents
are:"+arl);
System.out.println("Size
is:"+arl.size());
Iterator it=arl.iterator();
while(it.hasNext())
{
System.out.println(it.next());
}
}
}
File Name: -
Arraylistdemo.java
Commands:-
javac Arraylistdemo.java
java Arraylistdemo
OUTPUT:-
Contents are:
[C,C++,Java,Oracle]
Size is: 4
Contents are:
[C,C++,Oracle]
Size is: 3
C
C++
Oracle
NOTE:-
Ø The initial
capacity of the arraylist is equal to the number of elements added in the
arraylist.
VECTOR
Ø The size of
the Vector increases dynamically at runtime.
Ø To create
object for Vector class
Vector v=new
Vector (size);
Ø To add
elements to Vector we have to execute add
()method.
v.add
(“item”);
Ø To know the
number of elements present in Vector we have to execute size () method.
v.size ();
Ø To know the
capacity of the Vector we have to execute capacity()
method
v.capacity (
);
Ø To know the
first element present in the Vector we have to execute firstElement () method
v.firstElement
();
Ø To know the
last element present in the Vector we have to execute lastElement () method
v.lastElement
();
PROGRAM:-
import
java.util.*;
class
Vectordemo
{
public static void main(String args[])
{
Vector v=new Vector(2);
int x[]={10,20,30,40,50};
for(int i=0;i
v.add(x[i]);
for(int i=0;i
System.out.println(v.get(i));
System.out.println("Capcity:"+v.capacity());
System.out.println("Size
is:"+v.size());
System.out.println("First Element:"+v.firstElement());
System.out.println("Last Element:"+v.lastElement());
ListIterator lit=v.listIterator();
System.out.println("Forward
direction");
while(lit.hasNext())
System.out.println(lit.next());
System.out.println("Reverse
direction");
while(lit.hasPrevious())
System.out.println(lit.previous());
}
}
File Name: -
Vectordemo.java
Commands:-
javac Vectordemo.java
java Vectordemo
OUTPUT:-
10
20
30
40
50
Capacity:8
Size is:5
First Element: 10
Last Element: 50
Forward direction
10
20
30
40
50
Reverse direction
10
20
30
40
50
NOTE:-
Ø The initial
capacity of the vector is 8 and the capacity is doubled when we add the ninth
element and so on…
Hashtable
Ø Elements are
stored in the form of key and value pairs.
Ø To create
object for Hashtable class
Hashtable ht=new Hashtable(size);
Ø To put
elements in the hashtable
ht.put(key,value);
Ø To get the
value of corresponding key
ht.get(key);
Ø To know
number of elements present in Hashtable
ht.size();
Ø To know
whether the hashtable is empty or not
ht.isEmpty();
Ø To know
whether the corresponding key is present in hashtable or not
ht.containsKey(key);
Ø To get the
keys present in hash table
ht.keys ();
Ø Initial
Capacity = 11
Ø Load Factor
= 0.75
Ø Load Factor
indicates that when the initial capacity is to be doubled
i.e. initial capacity * load factor=
11*0.75=8.25
Ø The capacity
of the Hashtable is doubled when eight elements are added in the hashtable.
PROGRAM:-
import
java.util.*;
import
java.io.*;
class
HTdemo
{
public static void main(String args[]) throws
Exception
{
Hashtable ht=new Hashtable(2);
ht.put("C",1000);
ht.put("C++",1200);
ht.put("CoreJava",1000);
ht.put("AdvJava",1250);
System.out.println(ht.size());
System.out.println(ht.isEmpty());
System.out.println(ht.containsKey("C"));
Enumeration e=ht.keys();
while(e.hasMoreElements())
System.out.println(e.nextElement());
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
System.out.println("Enter course
name");
String name=br.readLine();
Integer price=(Integer)ht.get(name);
if(price!=null)
System.out.println(name+" price is
"+price);
else
System.out.println("course not
offered");
}
}
File Name: -
HTdemo.java
Commands:-
javac HTdemo.java
java HTdemo
OUTPUT:-
4
false
true
CoreJava
AdvJava
C++
C
Enter course name
CoreJava
CoreJava price is 1000
HashMap
Ø Elements are
stored in the form of key and value pairs.
Ø To create
object for HashMap class
HashMap hm=new HashMap(size);
Ø To put
elements in the HashMap
hm.put(key,value);
Ø To get the
value of corresponding key
hm.get(key);
Ø To know
number of elements present in HashMap
hm.size();
Ø Initial
Capacity = 16
Ø Load Factor
= 0.75
Ø Load Factor
indicates that when the initial capacity is to be doubled
i.e. initial capacity * load factor= 16*0.75=12
Ø The capacity
of the HashMap is doubled when 12 elements are added in the HashMap.
PROGRAM:-
import
java.util.*;
import
java.io.*;
class
HMdemo
{
public static void main(String args[]) throws
Exception
{
HashMap hm=new HashMap(2);
String name,pass;
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
while(true)
{
System.out.println("1.Enter User
Name and Password");
System.out.println("2.Get the
password");
System.out.println("3.Exit");
int n=Integer.parseInt(br.readLine());
switch(n)
{
case 1:
System.out.println("Enter username");
name=br.readLine();
System.out.println("Enter password");
pass=br.readLine();
hm.put(name,pass);
break;
case 2: System.out.println("Enter
username");
name=br.readLine();
pass=(String)hm.get(name);
System.out.println(pass);
break;
case 3: System.exit(0);
}
}
}
}
File Name: -
HMdemo.java
Commands:-
javac HMdemo.java
java HMdemo
OUTPUT:-
1.Enter User Name and
Password
2.Get the password
3.Exit
1
Enter User Name and
Password
Kiran
Kumar
1.Enter User Name and
Password
2.Get the password
3.Exit
2
Kiran
Kumar
1.Enter User Name and
Password
2.Get the password
3.Exit
2
hari
null
1.Enter User Name and
Password
2.Get the password
3.Exit
3
ResourceBundle
Ø This class
is present in java.util package.
Ø This class
is used to load the properties file.
Ø The properties file consists of key=value pairs.
Ø First we
have to create the properties file.
info.properties file:
course1=Core
Java Information
course2=Advance
Java Information
course3=J2EE
Information
PROGRAM:-
import
java.util.*;
class
Propertiesdemo
{
public static void main(String args[])
{
ResourceBundle
rb=ResourceBundle.getBundle("info");
System.out.println(rb.getString("course1"));
System.out.println(rb.getString("course2"));
System.out.println(rb.getString("course3"));
}
}
File Name: -
Propertiesdemo.java
Commands:-
javac Propertiesdemo.java
java Propertiesdemo
OUTPUT:-
Core Java Information
Advance Java
Information
J2EE Information