| | |
    Java Beginner Home
    Table of Contents
    Introduction to Java
    Getting Started with Java
    Basic Language Elements
    Java Operators
    Java Control Statements
    Java Access Modifiers
    Classes and Objects
    Java Constructors
    Object Serialization
    Java Class Inheritance
    Java Object Type Casting
    Abstract class and Interface
    Java Method Overiding
    Java toString Method
    Java String Class
    Java String Comparison
    Java StringBuffer
    Java Exceptions
    Singleton Pattern
    Java Threads Tutorial
    Java Collections Framework
    Java Date Util
    Swing Tutorial
    Download Java Software
    Advertise
    Feedback
    Java books
    Eclipse Plugin Site

Java Collections Framework



A collection represents a group of objects, known as its elements. This framework is provided in the java.util package. Objects can be stored, retrieved, and manipulated as elements of collections. Collection is a Java Interface. Collections can be used in various scenarios like Storing phone numbers, Employee names database etc. They are basically used to group multiple elements into a single unit. Some collections allow duplicate elements while others do not. Some collections are ordered and others are not.

A Collections Framework mainly contains the following 3 parts

A Collections Framework is defined by a set of interfaces, concrete class implementations for most of the interfaces and a set of standard utility methods and algorithms. In addition, the framework also provides several abstract implementations, which are designed to make it easier for you to create new and different implementations for handling collections of data.

Core Collection Interfaces

The core interfaces that define common functionality and allow collections to be manipulated independent of their implementation.
The 6 core Interfaces used in the Collection framework are:

  • Collection
  • Set
  • List
  • Iterator (Not a part of the Collections Framework)
  • SortedSet
  • Map
  • SortedMap

Note: Collection and Map are the two top-level interfaces.

Collection Interface

Map Interface

Concrete Classes

The concrete classes that are specific implementations of the core interfaces, providing data structures that a java program can use.

Note: Concrete Classes for the Map is shown in the previous section.
Standard utility methods and algorithms

Standard utility methods and algorithms

that can be used to perform various operations on collections, such as sorting, searching or creating customized collections.

How are Collections Used

  • The collections stores object references, rather than objects themselves. Hence primitive values cannot be stored in a collection directly. They need to be encapsulated (using wrapper classes) into an Object prior to storing them into a Collection (such as HashSet, HashMap etc).
  • The references are always stored as type Object. Thus, when you retrieve an element from a collection, you get an Object rather then the actual type of the collection stored in the database. Hence we need to downcast it to the Actual Type while retrieving an element from a collection.
  • One of the capabilities of the Collection Framework is to create a new Collection object and populate it with the contents of an existing Collection object of a same or different actual type.

Below is an example program showing the storing and retrieving of a few Collection Types

import java.util.*;

public class CollectionsDemo {

	public static void main(String[] args) {
		List a1 = new ArrayList();
		a1.add("Beginner");
		a1.add("Java");
		a1.add("tutorial");
		System.out.println(" ArrayList Elements");
		System.out.print("\t" + a1);
		List l1 = new LinkedList();
		l1.add("Beginner");
		l1.add("Java");
		l1.add("tutorial");
		System.out.println();
		System.out.println(" LinkedList Elements");
		System.out.print("\t" + l1);
		Set s1 = new HashSet(); // or new TreeSet() will order the elements;
		s1.add("Beginner");
		s1.add("Java");
		s1.add("Java");
		s1.add("tutorial");
		System.out.println();
		System.out.println(" Set Elements");
		System.out.print("\t" + s1);
		Map m1 = new HashMap(); // or new TreeMap() will order based on keys
		m1.put("Windows", "98");
		m1.put("Win", "XP");
		m1.put("Beginner", "Java");
		m1.put("Tutorial", "Site");
		System.out.println();
		System.out.println(" Map Elements");
		System.out.print("\t" + m1);
	}
}

Output

ArrayList Elements
[Beginner, Java, tutorial]
LinkedList Elements
[Beginner, Java, tutorial]
Set Elements
[tutorial, Beginner, Java]
Map Elements
{Tutorial=Site, Windows=98, Win=XP, Beginner=Java}

Download CollectionsDemo.java

Java Collections Source Code Examples

On the following pages in this tutorial I have described how elements can be manipulated by different collections namely;

 
 
    Java is a trademark of Sun Microsystems, Inc.
| | |
© Copyright 2007-08 javabeginner.com