Collection in Java

Collections Framework Introduction

 
Here you will learn what 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 Collection?

 
A collection sometimes called a container. It is the collection of objects known as its elements, collect into a single place and treated as a single unit. these objects can be a store, retrieve, manipulate 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.

  1.   Interfaces
  2.   Classes
Classes implement these interfaces that are found in java.util package. 
 

Interfaces

Collection-Interfaces.gif
 

In the Collections Framework, the interfaces Map and Collection are distinct with no lineage in the hierarchy. The typical application of the 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
 
The description of these interfaces is given below.
 
Interface-Description.gif
 
Corresponding Implementations
 
Corresponding-Implementatio
 
Interface-and-Classes.gif
 
Methods Of collection
 
Methods-of-collection.gif
 
Example
 
In this program, we use Array List Class and.
 
Create a Java program by using Eclipse ID. Open the Eclipse and Follow these steps
 
Step-1
 
Create a Java project and give the name 
 
Step-1.gif
 
Step-2
 
Here the name is Collection Demo.
 
Step-2.gif
 
Step-3
 
Create the Class and gave the name
 
Step-3.gif
 
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. 
  1. public boolean add(Object o)  
  2. we also use remove() and size() method in this program.import java.util.ArrayList;  
  3. public class CollectionDemo {  
  4.  public static void main(String arg[]) {  
  5.   @SuppressWarnings("rawtypes")  
  6.   ArrayList < Comparable > al = new ArrayList < Comparable > ();  
  7.   System.out.println("initial state array list size=" + al.size());  
  8.   al.add("b");  
  9.   al.add("h");  
  10.   al.add(new Integer(10));  
  11.   al.add("a");  
  12.   al.add("c");  
  13.   al.add("g");  
  14.   al.add("f");  
  15.   System.out.println("after inserting 7 elements array list size=" + al.size());  
  16.   System.out.println("printing array list items =" + al);  
  17.   al.remove(3);  
  18.   System.out.println("after deleting 1 element from the array list then size =" + al.size());  
  19.   System.out.println("printing array list items =" + al);  
  20.  }  
  21. }  
Step-5
 
Compile and run the program, the output of the program is given below.
 
Outputof-CollectionDemo.gif                     

 
Conclusion