My other sites

Latest News

New Summer Look :)










Package java.util

 

java.util.Vector

java.util.Stack

Stack Example

java.util.Enumeration

java.util.Hashtable

java.util.Random

java.util.StringTokenizer

 


java.util.Vector

The Vector class implements an expandable container for storing objects

Vector vect = new Vector();
vect.addElement("Hi");
vect.addElement(new Date());
vect.addElement("Bye");
System.out.println("Vector size: " + vect.size());
Object obj = vect.get(1);
if (obj instanceof String)
   System.out.println("String: " + (String)obj);
else if(obj instanceof Date)
   System.out.println("Date:" + (Date)obj);


java.util.Stack

The Stack extends the Vector with stack functionality

boolean empty() : Tests if this stack is empty

Object peek() : Looks at the object at the top of this stack without removing it

Object pop() : Removes the object at the top of this stack and returns that object as the value of this function

void push(Object) : Pushes an item onto the top of this stack

int search(Object): Returns where an object is on this stack


Stack Example

Stack stack = new Stack();
Object ob1 = new Object();
Object ob2 = new Object();
stack.push(ob1);
stack.push(ob2);
Object top = stack.peek(); // top is equal to ob2
Object tmp = stack.pop();  // tmp is also equal to ob2
Object tmp = stack.pop();  // tmp is now equal to ob1
if (stack.emtpy())
   System.out.println("Stack is empty");


java.util.Enumeration

An object that implements the Enumeration interface generates a series of elements, one at a time. Successive calls to the nextElement method return successive elements of the series Vector

vect = new Vector();
vect.addElement("Hi");
vect.addElement("Blipp");
vect.addElement("Bye");
for (Enumeration e=vect.elements();e.hasMoreElements();)
   System.out.println(e.nextElement());


java.util.Hashtable

This class implements a hashtable, which maps keys to values
Any non-null object can be used as a key or as a value

Hashtable table = new Hashtable();
Object ob1 = new Object();
Object ob2 = new Object();
table.put("1",ob1);
table.put("2",ob2);
.
. Object tmp = table.get("2");


java.util.Random

An instance of this class is used to generate a stream of pseudo random numbers

Random rand = new Random();
rand.nextInt();
rand.nextFloat();
...

 

java.util.StringTokenizer

- The StringTokenizer class allows a string to be broken into Tokens

- Operates using a delimiter character


String data = "This is some data";
StringTokenizer st = new StringTokenizer(data);
while (st.hasMoreTokens())
println(st.nextToken());