- Collection is the main interface.
- There are many methods declared in Collection interface. These are the common methods for all collections to perform different operations.
- public interface Collection<E>
- extends Iterable<E>
Methods in Collection Interface:1.public Boolean add(Object obj)
- Used to add element in to the collection
2.public Boolean addAll(Collection c)
- Adds all the elements of c to the invoking collection. Returns true if the operation succeeded else returns false.
3.public boolean isEmpty()
- Returns true if collection is empty otherwise returns false.
4.public boolean remove(Object obj)
- Remove element from collection. Returns true if successful deletion.
5.public boolean removeAll(Collection c)
- Removes all elements of a collection.
6.public void clear()
- Deletes total elements from collection
7.public boolean contains(Object obj)
- This method used to search an element
8.public boolean containsAll(Collection c)
- This method used to search an element in a collection
9.public boolean retianAll(Collection c)
- Used to delete all elements from a collection except specified one.
10.public int size()
- Returns total numbers of elements in a collection
11.public Iterator iterator()
- Returns iterator for the collection
12.public boolean equals(Object obj)
- Compares two collections
13.public int hashCode()
- Returns hashcode
14.public Object toArray()
- This method used to convert collection into array
- Returns an array containing only those collection elements whose type matches that of array.
Difference Between Collection and Collections:
- Collection is the base interface for most of the classes.
- Collections is the utility class.