Collections in Java

    0
    28

    The collection is a type of container that holds multiple elements in a single unit. The Java Collection Framework in the java.util package has a lot of interfaces and utilities.

     

    The difference between the collection, Collection, and Collections:

    collection: It represents any data structure in which objects are stored and iterated.

    Collection: It is java.util.Collection interface. Set, List, and Queue extend from this Interface. This interface declares various methods like add(), remove(), size(), contains() and iterator().

    Collections: Its a java.util.Collections class that contain static utility methods for use with Java collection Framework.

     

    Java Collection Framework:

    The Java Collection Framework is a combined architechture for representing and manipulating collections. All collections framework has Interfaces, Implementations, and Algorithms.

    Interfaces: Being abstract they allow collections to be manipulated independently.

    Implementation: also called the class in java. These are the reusable implementation of collection interface.

    Algorithm: These are useful computations, usually written in the form of methods. These methods are written only once and can be used whenever and wherever needed hence called polymorphic and also reusable. example: Sorting, Searching, etc.

    Operations on Collection:

    1. Add objects to the Collection.
    2. Remove objects from the collection.
    3. Find if an object (or group of objects) is contained in the Java collection Framework.
    4. Retrieve an object from the collection.
    5. Iterate through the collection.

     

    Key Classes and Interfaces

    Core Interfaces

    They encapsulate different types of collections.

    Core Concrete Classes


    Not all the collections in the Java Collection Framework implement the Collection Interface. Specifically, none of the Map related classes and interfaces extend from Collection. Therefore, HashTable, HashMap, SortedMap, LinkedHashMap and TreeMap do not extend from Collection.

    The class and interface hierarchy for collections

    Note that all the core collection interfaces are generic. A generic type is a generic class or interface that is parameterized over types. For example, consider the following declaration of Collection interface.
    public interface Collection<E>...
    where <E> tells that the interface is generic. While creating an instance of Collection the type of object contained must be specified as this would help the compiler to check if the object you are putting into the collection is correct.

     

    Exceptions in collections:

    UnsupportedOperationException

    Thrown when a collection cannot be modified.

    ClassCastExeption

    Thrown when one object is incompatible with other.

    NullPointerException

    Thrown when an attempt is made to store a null object in a Collection.

    IllegalArgumentException

    Thrown when an invalid argument is used

    Methods common to all collections:

    Methods Description
    boolean add(Object obj) Adds objects to the collection
    boolean addAll(Collection c) Adds all objects of the collection c
    to the invoking collection
    void clear() Clears all the elements from
    the invoking collection
    boolean remove(Object  obj) removes an object from collection
    boolean removeAll(Collection c) removes all objects of collection c
    from the invoking collection
    boolean contains(Object  obj) To check whether the object specifies
    is present in collection or not
    boolean containsAll(Collection c) To check if all the elements of
    collection c are present in the invoking
    collection or not
    boolean retainAll(Collection c) removes all the elements from the
    invoking collection expect those in
    collection c
    boolean isEmpty() returns true is collection is empty
    otherwise false
    int size() returns the number of elements
    present in the collection

    LEAVE A REPLY

    Please enter your comment!
    Please enter your name here