SEMrush

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

No.
Method
Description
1
public boolean add(Object element)
is used to insert an element in this collection.
2
public boolean addAll(collection c)
is used to insert the specified collection elements in the invoking collection.
3
public boolean remove(Object element)
is used to delete an element from this collection.
4
public boolean removeAll(Collection c)
is used to delete all the elements of specified collection from the invoking collection.
5
public boolean retainAll(Collection c)
is used to delete all the elements of invoking collection except the specified collection.
6
public int size()
return the total number of elements in the collection.
7
public void clear()
removes the total no of element from the collection.
8
public boolean contains(object element)
is used to search an element.
9
public boolean containsAll(Collection c)
is used to search the specified collection in this collection.
10
public Iterator iterator()
returns an iterator.
11
public Object[] toArray()
converts collection into array.
12
public boolean isEmpty()
checks if collection is empty.
13
public boolean equals(Object element)
matches two collection.
14
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()