codescracker


java

Java Collections Framework



« Previous Tutorial Next Tutorial »


The collections framework in Java, standardizes the way in which groups of objects are handled by your Java programs. When JDK 5 was released, some fundamental changes were made to collections framework, increases its power and streamlined its used:

Java Collections Example

Here is an example program, demonstrates the concept and use of collections in Java. This program uses Hashtable to store the names of bank depositors and their current balances:

/* Java Collections - Example Program */

import java.util.*;

class JavaCollectionExample
{
    public static void main(String args[])
    {
        Hashtable<String, Double> balance= new Hashtable<String, Double>();
        Enumeration<String> names;
        
        String str;
        double bal;
        
        balance.put("Deepak Patel", 3243.53);
        balance.put("Devraj Singh", 322.43);
        balance.put("Rajat Patel", 4323.00);
        balance.put("Vikrant Patel", 43.54);
        balance.put("Ravi Patel", -43.04);
        
        names = balance.keys();
        while(names.hasMoreElements())
        {
            str = names.nextElement();
            System.out.println(str + ": " + balance.get(str));
        }
        System.out.println();
        bal = balance.get("Deepak Patel");
        balance.put("Deepak Patel", bal+1000);
        System.out.println("Deepak Patel's new balance: " + balance.get("Deepak Patel"));
    }
}

Here is the sample output produced by the above Java Program:

java collection

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test