May 15, 2016

Java Collections

Java Collection API

what is collection ?



collection is group of items. but grouping can be arrage different way and we can access items deferent way. 

What is the java collections ?


Same thing like group of objects, we can group objects and arrage that grouping differently. This arragement can be 
  • Uniqueness - when grouping objects group can not be repeat same object
  • Order - grouping object access by order
  • Process - how the grouping objects add, remove, search
  • Mapping - relations between grouping objects
So Java provide implemented collections to us.

Collections in Java 



These are the basic collection interfaces. As i described these collection arranged for different purposes.  we'll discuss those are later. in here Set, List and queue interfaces extends the Collection interface. These interface fall under java.utill package. Lets look at Collection interface

public interface Collection extends Iterable {

    int size();

    boolean isEmpty();

    boolean contains(Object o);

    Iterator iterator();

    Object[] toArray();

     T[] toArray(T[] a);

    boolean add(E e);

    boolean remove(Object o);

    boolean containsAll(Collection c);

    boolean addAll(Collection c);

    boolean removeAll(Collection c);

    default boolean removeIf(Predicate filter) {
        Objects.requireNonNull(filter);
        boolean removed = false;
        final Iterator each = iterator();
        while (each.hasNext()) {
            if (filter.test(each.next())) {
                each.remove();
                removed = true;
            }
        }
        return removed;
    }

    boolean retainAll(Collection c);

    void clear();

    boolean equals(Object o);

    int hashCode();
    
    default Spliterator spliterator() {
        return Spliterators.spliterator(this, 0);
    }

    default Stream stream() {
        return StreamSupport.stream(spliterator(), false);
    }

    default Stream parallelStream() {
        return StreamSupport.stream(spliterator(), true);
    }
}
This is the Collection interface.I'll explain few methods.
  • boolean add(Object obj) Adds object collection and returns true if the object was added to the collection. Returns false if the object is already added to the collection, or if the collection does not allow duplicates.
  • boolean addAll(Collection c) Adds all the elements of collection c to the invoking collection. Returns true if the elements were added. Otherwise, returns false.
  • boolean remove(Object obj) Removes instance obj from the invoking collection. Returns true if the element was removed. Otherwise, false.
  • boolean removeAll(Collection c) Removes all elements of Collection c from the invoking collection. Returns true if the celements were removed. Otherwise, false.
  • void clear( ) Removes all elements from the invoking collection
  • boolean contains(Object obj) Returns true if obj is an element of the invoking collection. Otherwise,false.
  • boolean containsAll(Collection c) Returns true if the invoking collection contains all elements of Collection c. Otherwise, false.
  • boolean equals(Object obj) Returns true if the invoking collection and obj are equal. Otherwise, false.
  • int hashCode( ) Returns the hash code for the invoking collection.
    We will discuss more about hashCode() later 
  • boolean isEmpty( ) Returns true if the invoking collection is empty. Otherwise, false.
  • int size( ) Returns the number of elements held in the invoking collection.

if you noticed that Collection Interface extends Iterable interface. So What is this Iterable ? :o
Iterable Interface This interface fall under java.lang package. If we implement this interaface in a class then we can use that class in for-each loop. like ,
   
      for(Object o : list){
        //do something o;    
      }
   
more about Iterable
We will discuss about subtypes of Collection interfaces Next ... :)