Tuesday, August 21, 2018

Java Collection Interface

| Tuesday, August 21, 2018
  • Collection is an interface inwards java.
  • Collection interface extends Iterable interface.
  • In hierarchy, Collection interface is rootage of List, Queue together with Set interfaces.

Collection interface extends Iterable interface Java Collection Interface

  • It represents unit of measurement of its elements. i.e. grouping of objects.
  • Some collections produce non permit duplicate elements exactly about allows duplicate elements.
  • Collection interface is base of operations on which collection framework is built.
  • Collection interface contains basic functioning methods every bit bellow.
  • boolean add(E element) - To brand certain that this collection contains the specified element. It volition supply truthful if object is added to collection. Return imitation if object is already at that spot together with collection does non permit duplicates.
  • boolean contains(Object element) - It volition supply truthful if collection already accept specified element.
  • boolean isEmpty() - Return truthful if collection is empty.
  • Iterator<E> iterator() - It volition supply an iterator for the invoking collection.
  • boolean remove(Object element) - It volition take away unmarried instance of given chemical part from collection. Return truthful if it is introduce else it volition supply false.
  • int size() - It volition supply seat out of elements introduce inwards invoking collection.
  •  Collection interface besides contains methods that operate on entire collections every bit bellow.
  • boolean addAll(Collection<? extends E> c) - It volition add together all elements of c to the invoking collection. Return truthful if functioning was success else it volition supply false.
  • boolean containsAll(Collection<?> c) - It volition supply truthful if this collection contains all the elements of specified collection.
  • void clear() - It volition take away all the elements from invoking collection.
  • boolean removeAll(Collection<?> c) - It volition take away all those elements from invoking collection which are available inwards c. It volition supply truthful if given elements removed else it volition supply false.
  • boolean retainAll(Collection<?> c) - Retains all the elements inwards invoking collection which are available inwards c.
  • Also at that spot are methods to perform array operations every bit bellow.
  • Object[] toArray() - Return array of elements from invoking collection.
  • <T> T[] toArray(T[] a) - Return array of all those elements from collection.
Basic Example of Collection
package JAVAExamples;  import java.util.ArrayList; import java.util.Collection; import java.util.LinkedList;  world shape CollectionExample {   world static void main(String[] args) {    // Collection ArrayList.   Collection<String> c1 = novel ArrayList<String>();   // Add items inwards ArrayList.   c1.add("ArrayList Item 1");   c1.add("ArrayList Item 2");   c1.add("ArrayList Item 3");   c1.add("ArrayList Item 4");   // Get collection Items list.   System.out.println("Collection Items are : " + c1);   // Get size of collection.   System.out.println("Size of collection is : " + c1.size());   // Remove exceptional from collection.   c1.remove("ArrayList Item 3");   System.out.println("Collection Items are : " + c1);    System.out.println();   System.out.println();           // Collection LinkedList.   Collection<String> c2 = novel LinkedList<String>();   // Add items inwards LinkedList.   c2.add("LinkedList Item 1");   c2.add("LinkedList Item 2");   c2.add("LinkedList Item 3");   c2.add("LinkedList Item 4");   // Get collection Items list.   System.out.println("Collection Items are : " + c2);   // Get size of collection.   System.out.println("Size of collection is : " + c2.size());   // Remove exceptional from collection.   c2.clear();   System.out.println("Collection Items are : " + c2);  } }

Output :
Collection Items are : [ArrayList Item 1, ArrayList Item 2, ArrayList Item 3, ArrayList Item 4] Size of collection is : four Collection Items are : [ArrayList Item 1, ArrayList Item 2, ArrayList Item 4]   Collection Items are : [LinkedList Item 1, LinkedList Item 2, LinkedList Item 3, LinkedList Item 4] Size of collection is : four Collection Items are : []


So this is basic regard of rootage collection interface. We volition larn dissimilar nestling interfaces of collection interface i past times i to buy the farm regard nearly how collection interface works.

Related Posts