Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


krishnarao
java
3 Answer(s)      6 years and 5 months ago
Posted in : Java Beginners



Advertisement
View Answers

August 11, 2008 at 3:51 PM



VECTOR implements a dynamic array.
It is similar to arraylist but with 2 diff....
VECTOR is synchronized..
It contains many legacy methods which are not part of the Collection framework.
From java2 onwards it is fully compatible with collections...



Hashtable is a concrete implementation of Dictionary
from java2 onwards it implements Map intrface.
Thus it is nw intgratd into collection.
Similar to HashMap, but is synchronized


hashtable stores key/ value pairs in a hash table



an eg to store the names
//demonstrate a hashtable

import java.util.*;
class HTDemo{
psvm(String a[])
{
HAshtable mark= new Hashtable();
Enumeration names;
String str;
double m;
bal.put("prashu",new double(37452.50));
bal.put("krishna",new double(37452.50));
//show all marks in hash table
names=bal.keys();
while(names.hasMoreElements())
{
str=(String)names.nextElement();
S.o.p(str+ ";"+bal.get(str));
}



names=bal.keys();


August 11, 2008 at 3:53 PM


hai...........
that last line is not there....names=bal.keys();
and i forgot to close the class and main

thanks and regards
prashu


August 11, 2008 at 4:14 PM


Hi friend,


Difference between ArrayList and Vector

ArrayList

Arraylist are the new collection and are more flexible than any other collection as arraylists are dynamic in nature. Since the methods of Arraylist are not synchronized therefore they are faster in performace.
Arraylist has no default size.
In ArrayList data is retrived through get() method.

Vector

Vectors are the old collections therefore they comes under the legacy classes.
They are synchronized. Vector has a default size of 10.
Vectors are used only in those conditions multiple threads are accessed simultaneously otherwise ArrayList is the best option. Vector grows dynamically. In vector the data is retrieve through elementAt() method.


import java.util.*;
public class VectorExample{
public static void main(String[] args){
Vector<Object> vector = new Vector<Object>();
int primitiveType = 10;
Integer wrapperType = new Integer(20);
String str = "Vineet Bansal";
vector.add(primitiveType);
vector.add(wrapperType);
vector.add(str);
vector.add(2, new Integer(30));
System.out.println("the elements of vector: " + vector);
System.out.println("The size of vector are: " + vector.size());
System.out.println("The elements at position 2 is: " + vector.elementAt(2));
System.out.println("The first element of vector is: " + vector.firstElement());
System.out.println("The last element of vector is: " + vector.lastElement());
vector.removeElementAt(2);
Enumeration e=vector.elements();
System.out.println("The elements of vector: " + vector);
while(e.hasMoreElements()){
System.out.println("The elements are: " + e.nextElement());
}
}
}


When there are certain need for thread-safe operation Vector needs to be used.


HashTable :Hash Table holds the records according to the unique key value.
It stores the non-contiguous key for several values.
Hash Table is created using an algorithm (hashing function)
to store the key and value regarding to the key in the hash bucket.

import java.util.*;
import java.io.*;

public class HashTableExample{
public static void main(String[] args) throws IOException{
int key;
try{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("How many elements you want to enter to the hash table : ");
int n = Integer.parseInt(in.readLine());
Hashtable<Integer, String> hashTable = new Hashtable<Integer, String>();
for(int i = 0; i < n; i++){
System.out.print("Enter key for the hash table : ");
key = Integer.parseInt(in.readLine());
System.out.print("Enter value for the key : ");
hashTable.put(key, in.readLine());
}
Map<Integer, String> map = new TreeMap<Integer, String>(hashTable);
System.out.println(map);
}
catch(NumberFormatException ne){
System.out.println(ne.getMessage() + " is not a legal value.");
System.out.println("Please enter a numeric value.");
System.exit(1);
}
}
}

Thanks


Related Tutorials/Questions & Answers:
java
java  diff bt core java and java
java
java  what is java
Advertisements
Java
Java   Whether Java is pure object oriented Language
java
java  different between java & core java
java
java  explain technologies are used in java now days and structure java
java
java   why iterator in java if we for loop
JAVA
JAVA  how the name came for java language as "JAVA
java
java  what is java reflection
java
java  is java open source
java
what is the size of array in java ?  what is the size of array in java ? what is the mean of finalize in java
java
java   in java does not pointers concept but what is nullpointers in java?   nullpointer is a runtime Exception
java
java  write a java program using filenotfoundexception
Java
Java  how to draw class diagrams in java
java
java   difference between class and interface
java
java  why multiple inheritance is not possible in java
java
java  explain object oriented concept in java
java
java  is java purely object oriented language
Java
Java   Whether Java is Programming Language or it is SOftware
java
java  what are JAVA applications development tools
java
java  how use java method
java
java  send me java interview questions
java
java  write a program in java to acess the email
java
java  Write a java code to print "ABABBABCABABBA
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  why to set classpath in java
java
java  give a simple example for inheritance in java
java
java  give a simple example for inheritance in java
Java
Java  how to do java in command prompt
java
java  Does java allows multiline comments
 

 

 

DMCA.com