Collection Interface

The collection interface is a root interface of java collections framework (java.util.Collection).Its general declaration is,

Interface Collection <E>

The core collection interfaces encapsulate different types of collections, which are shown in the figure below.

Collection Interface
Collection Interface

 

It declares the core methods that all collections will have. These methods are summarized in the following table.

  • public boolean add(Object obj): It is used to insert an element in collection. It returns true if element is inserted to the collection. Returns false if element is already a member of the collection, or if the collection does not allow duplicates.
  • public boolean addAll(Collection c): It is used to inserts all the elements in collection. Returns true if insert operation succeed, otherwise returns fails.
  • public boolean remove(Object obj): It is used to remove an element from this collection. Returns true if element is removed, otherwise returns false.
  • public boolean removeAll(Collection c): It is used to delete all the elements of specified collection from the invoking collection.
  • public void clear(): This Method used to delete all the elements in the collection.
  • public int size(): Return the total number of elements in the collection.
  • public boolean contain(Object obj): This method return true if Collection contain all the element of C, otherwise returns false.
  • public boolean containAll(Collection c): Return the total number of elements in the collection.
  • public boolean equals(Object obj): Returns true if the invoking collection and obj are equal. Otherwise, returns false.
  • public int hashcode(): Returns the hashcode number for collection.
  • public Boolean isEmpty(): Returns true if collection is empty, otherwise false.
  • public Iterator iterator( ): Returns an iterator.
  • public boolean retainAll(Collection c): This is used to delete all the elements of invoking collection except the specified collection c.
  • public Object[ ] toArray( ): Converts collection to array.

Related Posts:-

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Show Buttons
Hide Buttons