Java collection framework tutorial


Java collection framework tutorial - contributed by Pradip Patil

Collection

Collection Overview:

The java.util package contains all the classes and interfaces related to the collection Framework. A collection represents a group of objects, known as its elements.

All general-purpose Collection implementation classes .Collections are primarily defined through a set of interfaces.

.

Collection interface: This enables you to work with group of objects; it is at the top of the collections hierarchy.

List interface: This extends Collection and an instance of List stores an ordered collection of elements

Set : This extends Collection to handle sets, which must contain unique elements

SortedSet : This extends Set to handle sorted sets

Map : This maps unique keys to values.

SortedMap : This extends Map so that the keys are maintained in ascending order.

The collection classes: Some of the collection classes are following

Class Description
AbstractList Extends AbstractCollection and implements most of the List interface
AbstractCollection Implements most of the Collection interface
LinkedList Implements a linked list by extending AbstractSequentialList.
ArrayList Implements a dynamic array by extending AbstractList.
HashSet Extends AbstractSet for use with a hash table.
TreeSet Implements a set stored in a tree. Extends AbstractSet.
HashMap Extends AbstractMap to use a hash table
TreeMap Extends AbstractMap to use a tree.
HashTable Hashtable was part of the original java.util and is a concrete implementation of a Dictionary.
Stack Stack is a subclass of Vector that implements a standard last-in, first-out stack.
Vector This implements a dynamic array. It is similar to ArrayList, but with some differences.

Iterator Interface: for Select each element in a collection

Eg.

import java.io.*;
import java.util.*;

class Student
{
             public static void main(String args[])throws IOException
{
             Hashtable hs=new Hashtable();
             BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
             System.out.println("Enter how many no of students:");
             int n=Integer.parseInt(br.readLine());
             for(int i=0;i<n;i++)
             {
                          System.out.println("Enter Name:");
                          String name=br.readLine();
                          System.out.println("Enter Percentage:");
                          float per=Float.parseFloat(br.readLine());
                          hs.put(name,per);
             }
             Enumeration keys=hs.keys();
             Enumeration values=hs.elements();
             System.out.println("Details of Student:");
             while(keys.hasMoreElements())
             {
                          System.out.println("Name:"+keys.nextElement());
                          System.out.println("Percentage:"+values.nextElement());
             }
                          System.out.println("Enter name of student to be serched:"); 
                          String name1=br.readLine();
                          keys=hs.keys();
                          values=hs.elements();
             while(keys.hasMoreElements())
             {
                          String str=(String)keys.nextElement(); 
                          if(str.equals(name1))
                          {
                                       System.out.println("Percentage of "+name1+" is:"+hs.get(name1));
                          }

             }


   }
}



Write your comment - Share Knowledge and Experience



 
Interview questions
Latest MCQs
» General awareness - Banking » ASP.NET » PL/SQL » Mechanical Engineering
» IAS Prelims GS » Java » Programming Language » Electrical Engineering
» English » C++ » Software Engineering » Electronic Engineering
» Quantitative Aptitude » Oracle » English » Finance
Home | About us | Sitemap | Contact us | We are hiring