Java Collections Framework

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:

  • Generics fundamentally changed the collections framework
  • Autoboxing facilitates the use of primitive types
  • The for-each style for loop

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();
            str = names.nextElement();
            System.out.println(str + ": " + balance.get(str));
        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

Java Online Test

« Previous Tutorial Next Tutorial »

Follow/Like Us on Facebook

Subscribe Us on YouTube