Modal Box

Collection in Java

In java, a collection is the group of objects which provide readymade architecture to manipulate them.
In java some important collection classes and interfaces are as following :

a. ArrayList
b. LinkedList
c. Vector
d. List

e. HashSet
f. HashMap
g. TreeSet
h. TreeMap etc.

To use collection, java.util package needs to be imported.
Advantage of collection over array is to provide dynamic memory allocations.

ArrayList in java :

Exmaple to show implementation of ArrayList.

class ArrayListDemo
{
 public static void main(String args[])
 {
   ArrayList list= new ArrayList();// Default Type array List is created.
   list.add("delhi"); // elements are added to the arraylist
   list.add("fbd"); 
   list.add("noida");
  
//loop to display elements in array list for(int i=0;i<list.size();i++) { System.out.println(list.get(0)); } } }
Similarly, we can create arrayList of other type such as string type etc.
 ArrayList<String> list = new ArrayList<String>();

Vector :

Implementation of Vector is shown below.

class VectorDemo
{
 public static void main(String args[])
 {
   Vector v = new Vector();
   v.addElement("Java");
   v.addElement("C++");
   v.addElement("Python");
   
for(int i=0; i<v.size();i++) { System.out.println("Item:"+v.get(i)); } } }

Video/ C Introduction

Watch video in full size
Wordpress Social Share Plugin powered by Ultimatelysocial