Collections in Java

What is Collections?
Collections are like containers that groups multiple items in a single unit. Example: a jar of chocolates, list of names etc.

What is a Java Collection Framework?
A Java collection framework provides an architecture to store and manipulate a group of objects. A Java collection framework includes the following:

Interfaces:
1) Java collection Interface provides the abstract data type to represent collection. 
2) java.util.Collection is the root interface of Collections Framework. It is on the top of Collections framework hierarchy. It contains some important methods such as size(), iterator(), add(), remove(), clear() that every Collection class must implement. 
3) Some other important interfaces are java.util.List, java.util.Set, java.util.Queue and java.util.Map. 
4) Map is the only interface that doesn’t inherits from Collection interface but it’s part of Collections framework. 
5) All the collections framework interfaces are present in java.util package

Implementation Classes:
1) Collections in Java provides concrete implementation classes for collections. We can use them to create different types of collections in java program.
2) Some important collection classes are ArrayList, LinkedList, HashMap, TreeMap, HashSet, TreeSet.
3) These classes solve most of our programming needs but if we need some special collection class, we can extend them to create our custom collection class.

Algorithm:
1) Algorithms are useful methods to provide some common functionalities, for example searching, sorting and shuffling.


Hierarchy of Collection Framework
Following is the hierarchy of collection Interface and map interface.The java.util package contains all the classes and interfaces for Collection framework.
Collection Interface

Map Interface

Collection Interface:
1) This is the root of the collection hierarchy. 
2) All the core collection interfaces are generic; for example public interface Collection<E>. The <E> syntax is for Generics and when we declare Collection, we should use it to specify the type of Object it can contain. 
3) The interface has methods to tell you how many elements are in the collection (size, isEmpty), to check whether a given object is in the collection (contains), to add and remove an element from the collection (add, remove), and to provide an iterator over the collection (iterator).
4) Collection interface also provides bulk operations methods that work on entire collection – containsAll, addAll, removeAll, retainAll, clear.

Java Collection methods

Map Interface
A Map is an object that maps keys to values. A map cannot contain duplicate keys: Each key can map to at most one value. The Map interface includes methods for basic operations (such as put, get, remove, containsKey, containsValue, size, and empty), bulk operations (such as putAll and clear), and collection views (such as keySet, entrySet, and values).

Iterator interface:
1) Iterator interface provides the facility of iterating the elements in forward direction only.
2) Following are the iterator methods
Java iterator methods



<-- Previous || Next -->

No comments:

Post a Comment