Monday, 10 October 2016

What is Collection Interface In Java ?

This blog is all about to learn Core Java Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.

Previously we have discussed about
What is Java Collections Framework ?
Here, we learn What is Collection Interface In Java.
Interface Collection<E>

Type Parameters:
E - the type of elements in this collection

All Superinterfaces:
Iterable<E>

All Known Subinterfaces:
BeanContext, BeanContextServices, BlockingDeque<E>, BlockingQueue<E>, Deque<E>, List<E>, NavigableSet<E>, Queue<E>, Set<E>, SortedSet<E>, TransferQueue<E>

All Known Implementing Classes:
AbstractCollection, AbstractList, AbstractQueue, AbstractSet, ArrayList, CopyOnWriteArrayList, CopyOnWriteArraySet, EnumSet, HashSet, LinkedHashSet, LinkedList, PriorityBlockingQueue, PriorityQueue, Stack, TreeSet, Vector

Declaration of Collection Interface:
public interface Collection<E>
extends Iterable<E>

Collection extends the Iterable interface.Here, E specifies the type of objects that the collection will hold. A collection represents a group of objects, known as its elements. 
Some collections allow duplicate elements and others do not. Some are ordered and others unordered.
The JDK does not provide any direct implementations of this interface: it provides its sub interfaces like Set and List. This interface is typically used to pass collections around and manipulate them where maximum generality is desired.
This interface is a member of the Java Collections Framework.

Collection interfaces defines some core interfaces which are summarized below :-


Interface
Description
Collection
This interface Enables you to work with groups of objects, it is at the top of the collections hierarchy.
This interface Extends Collection to handle list of objects.
This interface Extends Collection to handle sets, which must contain unique elements.
This interface Extends Collection to handle special types of lists in which elements are removed only from the head. As FIFO(First Come First Out).  
This interface Extends Queue to handle a double-ended queue.
This interface Extends Set to handle sorted sets.
This interface Extends SortedSet to handle retrieval of elements based on closest-match searches.

Method Summary of Collection Interface :-
Collection declares some methods that all collections will have,these methods are summarized below:-

Method
Description
boolean add(E e)
Ensures that this collection contains the specified element (optional operation).
boolean addAll(Collection<? extends E> c)
Adds all of the elements in the specified collection to this collection (optional operation).
void clear()
Removes all of the elements from this collection (optional operation).
boolean contains(Object o)
Returns true if this collection contains the specified element.
boolean containsAll(Collection<?> c)
Returns true if this collection contains all of the elements in the specified collection.
boolean equals(Object o)
Compares the specified object with this collection for equality.
int hashCode()
Returns the hash code value for this collection.
boolean isEmpty()
Returns true if this collection contains no elements.
Iterator<E> iterator()
Returns an iterator over the elements in this collection.
boolean remove(Object o)
Removes a single instance of the specified element from this collection, if it is present (optional operation).
boolean removeAll(Collection<?> c)
Removes all of this collection's elements that are also contained in the specified collection (optional operation).
boolean retainAll(Collection<?> c)
Retains only the elements in this collection that are contained in the specified collection (optional operation).
int size()
Returns the number of elements in this collection.
Object[] toArray()
Returns an array containing all of the elements in this collection.
<T>T[]  toArray(T[] a)
Returns an array containing all of the elements in this collection; the runtime type of the returned array is that of the specified array.

Traversing Collection :-
There are 3 ways to traverse collections:
1 ) using for-each 
2 ) using Iterator 

Example to Traverse Collection :-

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class TraversingCollectionExample {

public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("pushkar");
list.add("john");
list.add("peter");
list.add("tom");
System.out.println("Traversing Collection Using For-Each Loop --");
for(String s : list){
System.out.println(""+s);
}
System.out.println("\nTraversing Collection Using Iterator Interface --");
Iterator<String> itr = list.iterator();
while(itr.hasNext()){
System.out.println(itr.next());
}
System.out.println("\nFrom Java 8 and Later,Traversing a Collection is Quite easy --");
System.out.println("First Way using Lambda Expression : ");
list.forEach(str -> System.out.println(str));
System.out.println("Second Way Using stream : ");
list.stream().filter(l -> l.length() == 4).forEach(l -> System.out.println(l));
}

}
Program Output :-

Traversing Collection Using For-Each Loop --
pushkar
john
peter
tom

Traversing Collection Using Iterator Interface --
pushkar
john
peter
tom

From Java 8 and Later,Traversing a Collection is Quite easy --
First Way using Lambda Expression : 
pushkar
john
peter
tom
Second Way Using stream : 
john


No comments:

Post a Comment