java 3 Answer(s) 4 years and 11 months ago
Posted in : Java Beginners
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); } } }
java
java hi im new to java plz suggest me how to master java....saifjunaid@gmail.com
java java How to set java Policy for applet using jdk 6
java
java dear,
i want a field for date picker using java/java script
java
java create java program for delete and update the details,without using database, just a normal java program
java
java why methods in java raise exceptions
Have a look at the following link:
Java Exceptions
java java code to search the nodes how to write the java code to search the nodes using routers
java java online telephone directory i need coding for online telephone directory..by using java....pls help me
java
java how to edit text document by using java
then how to edit starting and ending of text document by using java
java
java different between java & core java
print("code sample
java
java different between java & core java
print("code sample
java java pattern code for a given words java pattern code for a given words pattern
java
java how can use sleep in java
which book learn of java language
java
java java swing
Swing is a principal GUI toolkit for the Java programming language. It is a part of the JFC (Java Foundation Classes), which is an API for providing a graphical user interface for Java programs
java
java how to prepare the java
Hi Friend,
If you want to learn how to install java, creating and running a java program then go through the following links:
http://www.roseindia.net/java/beginners/index.shtml
http
java
java what is the need of java if java is not there what will happen... work unless you have Java installed, and more are created every day. Java... to scientific supercomputers, cell phones to the Internet, Java is everywhere!
http
java
java 1.what type of inheritance supported in java.
2.what is the advantage of java over C++.
Hi Friend,
Java supports following... inheritance
Advantages of Java over C++:
1)Java is pure object oriented
java
java how to run applets in java
Hi,
In Java you can use Applet viewer to run the applet. Read more at Java Applet Viewer tutorial page.
Thanks
java
java what is javaJava is a programming language..., and business applications.
Advantages of Java:
* Java is simple, easy to design... programming languages.
* Java is object-oriented, that is used to build modular
java
java i need to draw a binary tree traversal in java applet