Tech Master Tutorials
Email Facebook Google LinkedIn Pinterest Twitter
Home Java Java 8 Java Interview Questions Java8 Interview Questions Object Oriented Programming in Java JVM Java Programming

Collections

A collection is a grouping of multiple objects. A collection can be used to store and manipulate any type of objects. Various collections utility classes are available in java to store objects and perform various operations on them. A collection can be used to group any kind of objects e.g Studets, Employees, Organizations etc.

Interfaces

:

Interfaces form the very foundation of the collectionframework.These are abstract data types that represent collections. Interfaces allow collections to be manipulated independently of the details of their representation. Also using interfaces as references to the concrete imlementations saves us the code changes due to changes in the implementation classes so it is recommended to use interfaces as reference.
Collection framework provides set of interfaces and classes which are child of Collection/Map interface. Collection is the most generic interface from the collections framework. On the other hand Map interface forms a different hierarchy as it is not a child of the Collection interface. All the collections interfaces are the child of the Collection interface except the Map interface. These two interface hierarchies have been defined in the following diagram.

collections-interfaces.png


Collection interface is the parent collection interface for other collection classes except the Map interface. Since the Map interface contains the key-value pair unlike other collections so Map does not fit the Collection interface hierarchy. Collection interface provides the most generic abstract methods for the collection interfaces.
Collection interface – Collection is the most generic interface which provides the generic methods for Set, List, Queue, Deque interfaces. Collection interface contains the following basic/aggregate operations/methods -


    int size()

    boolean isEmpty()

    boolean contains(Object element) 

    boolean add(E element)

    boolean remove(Object element) 

    Iterator iterator()

    boolean containsAll(Collection c)

    boolean addAll(Collection c) 

    boolean removeAll(Collection c)

    boolean retainAll(Collection c)

    void clear()
For more details go to link - https://docs.oracle.com/javase/tutorial/collections/interfaces/collection.html