Search This Blog

Thursday 2 March 2017

Collection Interfaces

Collection Interfaces
The core collection interfaces encapsulate different types of collections. These interfaces allow collections to be manipulated independently of the details of their representation. Core collection interfaces are the foundation of the Java Collections Framework.

The following describes the core collection interfaces:
  • Collection — Collection is the root of the collection hierarchy. A collection represents a group of objects known as its elements. Some types of collections allow duplicate elements, and others do not. Some are ordered and others are unordered. The Java platform provides implementation of more specific sub interfaces like Set and List and doesn't provide any direct implementations of Collection interface.
  • Set — Set is a collection that cannot contain duplicate elements.
  • List — List is an ordered collection. Lists can contain duplicate elements.
  • Queue — Queue is a collection used to hold multiple elements prior to processing.
  • Deque — Deque is a collection used to hold multiple elements prior to processing. In a deque all new elements can be inserted, retrieved and removed at both ends.
  • Map — Map is an object that maps keys to values. A Map cannot contain duplicate keys; each key can map to at most one value.
  • SortedSet — SortedSet is a Set that maintains its elements in ascending order.
  • SortedMap — SortedMap is a Map that maintains its mappings in ascending key order.
The Collection interface contains methods that perform basic operations, such as 
·        int size()
·        boolean isEmpty()
·        boolean contains(Object element)
·        boolean add(E element)
·        boolean remove(Object element)
·        Iterator<E> iterator()

It also contains methods that operate on entire collections
·        boolean containsAll(Collection<?> c)
·        boolean addAll(Collection<? extends E> c)
·        boolean removeAll(Collection<?> c)
·        boolean retainAll(Collection<?> c)
·        void clear().
Traversing Collections
There are three ways to traverse collections
1.     using aggregate operations
2.     using the for-each construct
3.     using Iterators

Aggregate Operations
In JDK 8 and later, the preferred method of iterating over a collection is to obtain a stream and perform aggregate operations on it.
Aggregate operations are often used in conjunction with lambda expressions to make programming more expressive, using less lines of code.
The following code sequentially iterates through a collection of Employees and prints out the Name of Employee of HR department
myEmployeeCollection.stream()
.filter(e -> e.getDepartment() == “HR”)
.forEach(e -> System.out.println(e.getName()));

for-each Construct
The for-each construct allows you to concisely traverse a collection or array using a for loop.
The following code uses the for-each construct to print out each element of a collection on a separate line.
for (Object o : collection)
    System.out.println(o);
           
Iterators
An Iterator is an object that enables you to traverse through a collection and to remove elements from the collection selectively.
You get an Iterator for a collection by calling its iterator method. The following is the Iterator interface.
public interface Iterator<E> {
    boolean hasNext();
    E next();
    void remove(); //optional
}
The hasNext method returns true if the iteration has more elements, and the next method returns the next element in the iteration.

The remove method removes the last element that was returned by next from the underlying Collection. 

Collection Interfaces
Collection Interfaces

No comments:

Post a Comment