|
|
| Util Package - Legacy Class
|
|
Home |
Vector | Stack |
Properties |
Hash Table | Dictionary
| Collections
|
|
Vector:
Dynamic growable array
The Vector class implements a growable array of objects. Like an array, it contains components that can be accessed using an integer index.
However, the size of a Vector can grow or shrink as needed to accommodate adding and removing items after the Vector has been created.
Each vector tries to optimize storage management by maintaining a capacity and a capacityIncrement. The capacity is always at least
as large as the vector size; it is usually larger because as components are added to the vector, the vector's storage increases in
chunks the size of capacityIncrement. An application can increase the capacity of a vector before inserting a large number of components;
this reduces the amount of incremental reallocation.
|
Example:
import java.util.Vector;
public class VectorExample {
public static void main(String[] args) {
Vector<String> vc=new Vector<String>();
// add vector elements
vc.add("Vector Object 1");
vc.add("Vector Object 2");
vc.add("Vector Object 3");
vc.add("Vector Object 4");
vc.add("Vector Object 5");
// add vector element at index
vc.add(3, "Element at fix position");
// vc.size() inform number of elements in Vector
System.out.println("Vector Size :"+vc.size());
// get elements of Vector
for(int i=0;i<vc.size();i++)
{
System.out.println("Vector Element "+i+" :"+vc.get(i));
}
}
}
Output:
Vector Size :6
Vector Element 0 :Vector object 1
Vector Element 1 :Vector object 2
Vector Element 2 :Vector object 3
Vector Element 3 :Element at fix position
Vector Element 4 :Vector object 4
Vector Element 5 :Vector object 5
|
Example:
import java.util.Iterator;
import java.util.Vector;
public class SimpleVectorExample {
public static void main(String[] args) {
//create a Vector object
Vector v = new Vector();
/*
Add elements to Vector using
*/
v.add("1");
v.add("2");
v.add("3");
System.out.println("Getting elements of Vector");
System.out.println(v.get(0));
System.out.println(v.get(1));
System.out.println(v.get(2));
}
}
Output:
Getting elements of Vector
1
2
3
|
Stack:
Last In First Out(LIFO)
The Stack class represents a last-in-first-out (LIFO) stack of objects. It extends class Vector with five operations that allow a vector
to be treated as a stack. The usual push and pop operations are provided, as well as a method to peek at the top item on the stack,
a method to test for whether the stack is empty, and a method to search the stack for an item and discover how far it is from the top.
|
Example:
//How do I use the Stack class in Java?
import java.util.Stack;
public class StackExample {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<Integer>();
// We stored some values in the stack object.
for (int i = 0; i < 10; i++) {
stack.push(i);
System.out.print(i + " ");
}
System.out.println("");
int position = stack.search(3);
System.out.println("Search result position: " + position);
// The current top value of the stack
System.out.println("Stack top: " + stack.peek());
// Here we popping out all the stack object items.
while (!stack.empty()) {
System.out.print(stack.pop() + " ");
}
}
}
Output:
0 1 2 3 4 5 6 7 8 9
Search result position: 7
Stack top:9
9 8 7 6 5 4 3 2 1 0
|
Properties:
Storing load with store
The Properties class represents a persistent set of properties. The Properties can be saved to a stream or loaded from a stream.
Each key and its corresponding value in the property list is a string.
A property list can contain another property list as its "defaults"; this second property list is searched if the property key is
not found in the original property list.
Because Properties inherits from Hashtable, the put and putAll methods can be applied to a Properties object. Their use is strongly
discouraged as they allow the caller to insert entries whose keys or values are not Strings. The setProperty method should be used
instead. If the store or save method is called on a "compromised" Properties object that contains a non-String key or value, the call
will fail.
» loading key/value pairs into a Properties object from a stream,
» retrieving a value from its key,
» listing the keys and their values,
» enumerating over the keys, and
» saving the properties to a stream.
|
Example:
import java.io.*;
import java.util.Properties;
public class PropertiesTest1 {
public static void main ( String [ ] args ) {
Properties props=new Properties ( ) ;
try {
props.load ( new FileInputStream ( new File ( "test.properties" ) ) ) ;
} catch ( IOException ie ) {
System.out.println ( "Error reading file" ) ;
}
String key = "name";
String val = props.getProperty ( key ) ;
System.out.println ( val ) ;
}
}
Output:
Error reading file
Null
|
Example:
import java.io.*;
import java.util.Properties;
public class PropertiesDemo
{
public static void main ( String args [ ] )
{
Properties oProperties=new Properties ( ) ;
String strFileName = "CMQueries.properties";
FileInputStream oFileStream = null;
try {
oFileStream = new FileInputStream ( strFileName ) ;
oProperties.load ( oFileStream ) ;
} catch ( IOException e ) {
//handle error
}
System.out.println ( oProperties.getProperty ( "KeyNotFound","Default value for this key" ) ) ;
}
}
Output:
Default value for this key
|
Hash Table:
Storing Key with Value
This class implements a hashtable, which maps keys to values. Any non-null object can be used as a key or as a value.
To successfully store and retrieve objects from a hashtable, the objects used as keys must implement the hashCode method and the equals method.
The initial capacity controls a tradeoff between wasted space and the need for rehash operations, which are time-consuming.
No rehash operations will ever occur if the initial capacity is greater than the maximum number of entries the Hashtable will
contain divided by its load factor. However, setting the initial capacity too high can waste space.
If many entries are to be made into a Hashtable, creating it with a sufficiently large capacity may allow the entries to be inserted
more efficiently than letting it perform automatic rehashing as needed to grow the table.
This example creates a hashtable of numbers. It uses the names of the numbers as keys:
Hashtable numbers = new Hashtable();
numbers.put("one", new Integer(1));
numbers.put("two", new Integer(2));
numbers.put("three", new Integer(3));
To retrieve a number, use the following code:
Integer n = (Integer)numbers.get("two");
if (n != null) {
System.out.println("two = " + n);
}
|
Example:
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;
import java.util.Map;
public class HashTableJava {
public static void main(String[] args) {
Hashtable<Integer,String> hTable=new Hashtable<Integer,String>();
hTable.put(new Integer(2), "Two");
hTable.put(new Integer(1), "One");
hTable.put(new Integer(4), "Four");
hTable.put(new Integer(3), "Three");
hTable.put(new Integer(5), "Five");
Set s =hTable.entrySet();
Iterator i=s.iterator();
while(i.hasNext())
{
Map.Entry m=(Map.Entry)i.next();
int key = (Integer)m.getKey();
String value=(String)m.getValue();
System.out.println("Key :"+key+" value :"+value);
}
}
}
Output:
Key :5 value :Five
Key :4 value :Four
Key :3 value :Three
Key :2 value :Two
Key :1 value :One
|
Example:
//How do I create a Hashtable and iterates its contents?
import java.util.Enumeration;
import java.util.Hashtable;
public class HashtableDemo {
public static void main(String[] args) {
// Creates an instance of Hashtable
Hashtable<String, Integer> numbers = new Hashtable<String, Integer>();
numbers.put("one", 1);
numbers.put("two", 2);
numbers.put("three", 3);
// Returns an enumeration of the keys in this Hashtable
Enumeration<String> keys = numbers.keys();
while (keys.hasMoreElements()) {
String key = keys.nextElement();
System.out.println("Key: " + key + ", Value: " + numbers.get(key));
}
}
}
Output:
Key : two, Value : 2
Key : one, Value : 1
Key : three, Value : 3
|
Dicitionary:
Storing Key with Value
The Dictionary class is the abstract parent of any class, such as Hashtable, which maps keys to values. Every key and every
value is an object. In any one Dictionary object, every key is associated with at most one value. Given a Dictionary and a key,
the associated element can be looked up. Any non-null object can be used as a key and as a value.
As a rule, the equals method should be used by implementations of this class to decide if two keys are the same.
|
| Back to top |
|