-
Notifications
You must be signed in to change notification settings - Fork 630
/
Copy pathJava-collection.java
41 lines (34 loc) · 1.18 KB
/
Java-collection.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Java program to demonstrate
// why collection framework was needed
import java.io.*;
import java.util.*;
class CollectionDemo {
public static void main(String[] args)
{
// Creating instances of the array,
// vector and hashtable
int arr[] = new int[] { 1, 2, 3, 4 };
Vector<Integer> v = new Vector();
Hashtable<Integer, String> h = new Hashtable();
// Adding the elements into the
// vector
v.addElement(1);
v.addElement(2);
// Adding the element into the
// hashtable
h.put(1, "geeks");
h.put(2, "4geeks");
// Array instance creation requires [],
// while Vector and hastable require ()
// Vector element insertion requires addElement(),
// but hashtable element insertion requires put()
// Accessing the first element of the
// array, vector and hashtable
System.out.println(arr[0]);
System.out.println(v.elementAt(0));
System.out.println(h.get(1));
// Array elements are accessed using [],
// vector elements using elementAt()
// and hashtable elements using get()
}
}