Collections Framework Introduction
Here you will learn what are collections are and how they can make your job easier and programs better.
The Collections Framework, first introduced with the Java 2 platform, Standard Edition, version 1.2. The Collections Framework provides a well-designed set of interfaces and classes for storing and manipulating groups of data as a single unit, a collection.
What is a Collection in Java?
A collection, sometimes called a container, is a collection of Objects known as elements, collected into a single place and treated as a single unit. These object can be stored, retrieved, manipulated as an element of a collection. Some collections allow duplicate elements and others do not. Some are ordered and others unordered.
The Two "standard" constructors should be provided by all the general-purpose Collection implementation classes.
A void (no arguments) constructor, which creates an empty collection, and a constructor with a single argument of type collection, which creates a new collection with the same elements as its argument.
The Java Collections Application Programming Interface (API) provide Java developers with a set of classes and interfaces for working with groups of objects. The different interfaces describe the different types of groups. For the most part, once you understand the interfaces, you understand the framework.
All collections frameworks contain the following:
- Interfaces:
These are abstract data types that represent collections. Interfaces allow collections to be manipulated independently of the details of their representation.
- Implementations:
These are the concrete implementations of the collection interfaces. In essence, they are reusable data structures.
- Algorithms:
These are the methods that perform useful computations, such as searching and sorting, on objects that implement collection interfaces.
Collections Framework are divided into two-part.
- Interfaces
- Classes
Classes implement these interfaces that are found in java.util package.
What are Interfaces in Java?
In the Collections Framework, the interfaces Map and Collection are distinct with no lineage in the hierarchy. The typical application of a map is to provide access to values stored by keys.
When designing software with the Collection Framework, it is useful to remember the following hierarchical relationship of the four basic interfaces of the framework.
-
The Collection interface is a group of objects, with duplicates allowed.
-
Set extends Collection but forbids duplicates.
-
List extends Collection also, allows duplicates and introduces positional indexing.
-
Map extends neither Set nor Collection
Description of these interfaces is given below.
Interfaces |
Description |
Concrete Classes |
Collection |
A basic interface that defines the normal operation that allows a collection of an object to maintain or handle as a single element. |
|
Set(Unique Element no order)
|
A set of unique elements. |
HashSet
LinkedHashSet |
SortedSet(Unique element but in sorted order)
|
A set in which the elements are stored in some order. |
TreeSet |
List(Duplicate elements but in insertion order) |
A sequence of elements need not be unique. |
Vector
ArrayList
LinkedList |
Map(Unique Keys) |
A basic interface that defines operation for maintaining the mapping of keys to values. |
HashMap
LinkedHashMap
HashTable |
SortedMap(Unique keys but in sorted order) |
This interface maintaining mapping in sorted in key order. |
TreeMap |
Corresponding Implementations
Here "i" means interface and "c" means class.
Methods Of collection
Example
In this program, we use Array List Class and create a Java program by using Eclipse IDE.
Open the Eclipse and Follow these steps:
Step 1: Create a Java project and give the name
Step 2: Here the name is Collection Demo.
Step 3: Create the Class by clicking class,a new window is open and gives the name of class.
Step 4: Here the name of the class CollectionDemo. In this program, we insert some object into the array list by using add() method of Collection interface. The syntax of add() method is defined below.
public boolean add(Object o)
we also use remove() and size()method in this program. here remove () remove the object into the ArrayList and size() find out the size of ArrayList.
- import java.util.ArrayList;
- public class CollectionDemo
- {
- public static void main(String arg[])
- {
- @SuppressWarnings("rawtypes")
- ArrayList<Comparable> al=new ArrayList<Comparable>();
-
-
- System.out.println("initial state array list size="+al.size());
- al.add("b");
- al.add("h");
- al.add(new Integer(10));
- al.add("a");
- al.add("c");
- al.add("g");
- al.add("f");
-
- System.out.println("after inserting 7 elements array list size="+al.size());
- System.out.println("printing array list items ="+al);
- al.remove(3);
- System.out.println("after deleting 1 element from the array list then size ="+al.size());
- System.out.println("printing array list items ="+al);
- }
- }
Step 5: Compile and run the program,the output of program is given below.
Summary
In the above article, we studied Collections in Java