17 Methods of Collections in java

What is Collection in java?

collection is an object that can hold references to other objects. The collection interface declares the operations that can be performed on each type of collection. The classes and interfaces of the collections framework are in package java.util.

Collection represents a single unit of objects i.e. a group. So basically Collections  is a framework/API  in java , which  provides an architecture to store and manipulate the group of objects.
Collection is an API in Java. This Collection provides a group of interfaces & also some direct classes.

Methods of Collection interface

public boolean add(Object element)
is used to insert an element in this collection.
public boolean addAll(collection c)
is used to insert the specified collection elements in the invoking collection.
public boolean remove(Object element)
is used to delete an element from this collection.
public boolean removeAll(Collection c)
is used to delete all the elements of specified collection from the invoking collection.
public boolean retainAll(Collection c)
is used to delete all the elements of invoking collection except the specified collection.
public int size()
return the total number of elements in the collection.
public void clear()
removes the total no of element from the collection.
public boolean contains(object element)
is used to search an element.
public boolean containsAll(Collection c)
is used to search the specified collection in this collection.
public Iterator iterator()
returns an iterator.
public Object[] toArray()
converts collection into array.
public boolean isEmpty()
checks if collection is empty.
public boolean equals(Object element)
matches two collection.
public int hashCode()
returns the hashcode number for collection.

Points to remember

·         Collections is a API that has interfaces and its implemented classes and also Algorithm
·          Methods declared in the Collection interface are
1.       add(Object element)
2.       addAll(collection c)
3.       remove(Object element)
4.       removeAll(Collection c)
5.       retainAll(Collection c)
6.       size()
7.       clear()
8.       contains(object element)
9.       containsAll(Collection c)
10.   iterator()
11.   toArray()
12.   isEmpty()
13.   equals(Object element)
14.   hashCode()

·         Iterator interface provides function declarations to iterate the elements in forward direction only.
·         Iterator interface has three methods only:
1.    hasNext()
2.    next()
3.    remove()