Skip to content

Commit caf87f5

Browse files
authored
Create Java-collection.java
1 parent 70c8c4e commit caf87f5

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

Java-collection.java

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

0 commit comments

Comments
 (0)