Java Collection Interface

  • Collection is an interface inward java.
  • Collection interface extends Iterable interface.
  • In hierarchy, Collection interface is origin of List, Queue too Set interfaces.

Java Collection Interface

  • It represents unit of measurement of its elements. i.e. grouping of objects.
  • Some collections create non permit duplicate elements simply roughly 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 provide truthful if object is added to collection. Return simulated if object is already at that spot too collection does non permit duplicates.
  • boolean contains(Object element) - It volition provide truthful if collection already convey specified element.
  • boolean isEmpty() - Return truthful if collection is empty.
  • Iterator<E> iterator() - It volition provide an iterator for the invoking collection.
  • boolean remove(Object element) - It volition take unmarried illustration of given chemical ingredient from collection. Return truthful if it is acquaint else it volition provide false.
  • int size() - It volition provide position out of elements acquaint inward invoking collection.
  •  Collection interface too 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 provide false.
  • boolean containsAll(Collection<?> c) - It volition provide truthful if this collection contains all the elements of specified collection.
  • void clear() - It volition take all the elements from invoking collection.
  • boolean removeAll(Collection<?> c) - It volition take all those elements from invoking collection which are available inward c. It volition provide truthful if given elements removed else it volition provide false.
  • boolean retainAll(Collection<?> c) - Retains all the elements inward invoking collection which are available inward 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;  populace shape CollectionExample {   populace static void main(String[] args) {    // Collection ArrayList.   Collection<String> c1 = novel ArrayList<String>();   // Add items inward 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 detail 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 inward 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 detail 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 : iv 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 : iv Collection Items are : []


So this is basic regard of origin collection interface. We volition acquire dissimilar nestling interfaces of collection interface 1 yesteryear 1 to live regard well-nigh how collection interface works.

More interesting articles here :Generation Enggelmundus Internet Marketing Tool here :Zeageat IM http://www.software-testing-tutorials-automation.com/
Post a Comment (0)
Previous Post Next Post