Tuesday, March 11, 2014

Core Java : Java Collection Basics


The Java platform includes a collections framework. A collection is an object that represents a group of objects (such as the classic Vector class).

A collection, as its name implied, is simply an object that holds a collection (or a group, a container) of objects. Each item in a collection is called an element. A framework, by definition, is a set of interfaces that force you to adopt some design practices. A well-designed framework can improve your productivity and provide ease of maintenance.
A collections framework is a unified architecture for representing and manipulating collections, enabling collections to be manipulated independently of implementation details.



The collection framework provides a unified interface to store, retrieve and manipulate the elements of a collection, regardless of the underlying and actual implementation. This allows the programmers to program at the interfaces, instead of the actual implementation.


The advantages of a collections framework
  1. Increases performance by providing high-performance implementations of data structures and algorithms.
  2. Reduces programming effort by providing data structures and algorithms so you don't have to write them yourself. 
  3. Reduces the effort required to design and implement APIs by not requiring you to produce ad hoc collections APIs. 
  4. Provides interoperability between unrelated APIs by establishing a common language to pass collections back and forth
     
    First Part of Collection Framework

    First Part of Collection Framework Related to List & Set

    Second Part Of Collection Framework Related to Map

    The following list describes the core collection interfaces:
Collection 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 doesn’t provide any direct implementations of this interface but provides implementations of more specific sub interfaces, such as Set and List.
 Set — It's come from mathematical word Set. This interface models the mathematical set abstraction and is used to represent sets. Set cannot contain duplicate elements
List — It is ordered collection like sequence . Lists can contain duplicate elements. The user of a List generally has precise control over where in the list each element is inserted and can access elements by their integer index (position).
Queue — a collection used to hold multiple elements prior to processing. Besides basic Collection operations, a Queue provides additional insertion, extraction, and inspection operations.
Queues typically, but do not necessarily, order elements in a FIFO (first-in, first-out) manner. Among the exceptions are priority queues, which order elements according to a supplied comparator or the elements’ natural ordering.
Map — an object that maps keys to values. A Map cannot contain duplicate keys; each key can map to at most one value. If you’ve used Hashtable, you’re already familiar with the basics of Map.

Deque — a collection used to hold multiple elements prior to processing. Besides basic Collection operations, a Deque provides additional insertion, extraction, and inspection operations. Deques can be used both as FIFO (first-in, first-out) and LIFO (last-in, first-out). In a deque all new elements can be inserted, retrieved and removed at both end.


 The last two core collection interfaces are merely sorted versions of Set and Map:
 SortedSet — a Set that maintains its elements in ascending order. Several additional operations are provided to take advantage of the ordering. Sorted sets are used for naturally ordered sets, such as word lists and membership rolls.
 SortedMap — a Map that maintains its mappings in ascending key order. This is the Map analog of SortedSet. Sorted maps are used for naturally ordered collections of key/value pairs, such as dictionaries and telephone directories.

Let's star to collect all features in the In-Depth 


Every Collection Object have different properties


Collection interface The Collection interface is used to represent any group of objects, or elements. You use the interface when you wish to work with a group of elements in as general a manner as possible. Here is a list of the public methods of Collection


 The interface supports basic operations like adding and removing. When you try to remove an element, only a single instance of the element in the collection is removed, if present.
 * boolean add(Object element)
 * boolean remove(Object element)


The Collection interface also supports query operations:
 * int size()
 * boolean isEmpty()
 * boolean contains(Object element)
 * Iterator iterator()



Iterator interface 
The iterator() method of the Collection interface returns an Iterator . An Iterator is similar to the Enumeration interface. You can traverse a collection from start to finish and safely remove elements from the underlying Collection

1:  Collection collection = ...;  
2:  Iterator iterator = collection.iterator();  
3:  while (iterator.hasNext())  
4:  {  
5:  Object element = iterator.next();  
6:  if (removalCheck(element)) {  
7:  iterator.remove();  
8:  }  
9:  }  



Set Interface  
The Set interface extends the Collection interface and, by definition, forbids duplicates within the collection. All the original methods are present and no new methods are introduced. The concrete Set implementation classes rely on the equals() method of the object added to check for equality. 

HashSet
This class implements the Set interface, backed by a hash table (actually a HashMap instance). Y
ou will use a HashSet for storing your duplicate-free collection.It makes no guarantees as to the iteration order of the set; in particular, it does not guarantee that the order will remain constant over time. This class permits the null element. For efficiency, objects added to a HashSet need to implement the hashCode() method in a manner that properly distributes the hash codes. While most system classes override the default hashCode() implementation in Object , when creating our own classes to add to a HashSet remember to override hashCode()  

1:  public class HashSet<E>  
2:    extends AbstractSet<E>  
3:     implements Set<E>, Cloneable, Serializable  

This class offers constant time performance for basic operations (add, remove, contains and size), assuming the hash function disperses the elements properly among the buckets. We can set the initial capacity and load factor for this collection. The load factor is a measure of how full the hash table is allowed to get before its capacity is automatically increased.


LinkedHashSet :
Hash table and linked list implementation of the Set interface, with predictable iteration order. This implementation differs from HashSet in that it maintains a doubly-linked list running through all of its entries. This linked list defines the iteration ordering, which is the order in which elements were inserted into the set (insertion-order). Note that insertion order is not affected if an element is re-inserted into the set. (An element e is reinserted into a set s if s.add(e) is invoked when s.contains(e) would return true immediately prior to the invocation).


This class provides all of the optional Set operations, and permits null elements. Like HashSet, it provides constant-time performance for the basic operations (add, contains and remove), assuming the hash function disperses elements properly among the buckets. 
Performance is likely to be just slightly below that of HashSet, due to the added expense of maintaining the linked list

A linked hash set has two parameters that affect its performance: initial capacity and load factor. They are defined precisely as for HashSet. Note, however, that the penalty for choosing an excessively high value for initial capacity is less severe for this class than for HashSet, as iteration times for this class are unaffected by capacity


Note that the fail-fast behavior of an iterator cannot be guaranteed as it is, generally speaking, impossible to make any hard guarantees in the presence of unsynchronized concurrent modification. Fail-fast iterators throw ConcurrentModificationException on a best-effort basis. Therefore, it would be wrong to write a program that depended on this exception for its correctness: the fail-fast behavior of iterators should be used only to detect bugs.


1:  public class LinkedHashSet<E>  
2:    extends HashSet<E>  
3:     implements Set<E>, Cloneable, Serializable  

Interface SortedSet
A SortedSet is a Set that maintains its elements in ascending order, sorted according to the elements' natural ordering or according to a Comparator provided at SortedSet creation time.Several additional operations are provided to take advantage of the ordering. Sorted sets are used for naturally ordered sets, such as word lists and membership rolls.

Range view — allows arbitrary range operations on the sorted set
Endpoints — returns the first or last element in the sorted set
Comparator access — returns the Comparator, if any, used to sort the set


1:  public interface SortedSet<E> extends Set<E> {  
2:    // Range-view  
3:    SortedSet<E> subSet(E fromElement, E toElement);  
4:    SortedSet<E> headSet(E toElement);  
5:    SortedSet<E> tailSet(E fromElement);  
6:    // Endpoints  
7:    E first();  
8:    E last();  
9:    // Comparator access  
10:    Comparator<? super E> comparator();  
11:  }  
The operations that SortedSet inherits from Set behave identically on sorted sets and normal sets with two exceptions:

  • The Iterator returned by the iterator operation traverses the sorted set in order.
  • The array returned by toArray contains the sorted set's elements in order.

SortedSet implementations also provide, by convention, a constructor that takes a Comparator and returns an empty set sorted according to the specified Comparator. If null is passed to this constructor, it returns a set that sorts its elements according to their natural ordering.

Range-view Operations

The range-view operations are somewhat analogous to those provided by the List interface, but there is one big difference. Range views of a sorted set remain valid even if the backing sorted set is modified directly. This is feasible because the endpoints of a range view of a sorted set are absolute points in the element space rather than specific elements in the backing collection.
The SortedSet interface contains two more range-view operations — headSet and tailSet, both of which take a single Object argument. The former returns a view of the initial portion of the backing SortedSet, up to but not including the specified object. The latter returns a view of the final portion of the backing SortedSet, beginning with the specified object and continuing to the end of the backing SortedSet. Thus, the following code allows you to view the dictionary as two disjoint volumes (a-m and n-z).
Endpoint Operations
The SortedSet interface contains operations to return the first and last elements in the sorted set, not surprisingly called first and last. In addition to their obvious uses, last allows a workaround for a deficiency in the SortedSet interface.
SortedSet<String> volume1 = dictionary.headSet("n");
SortedSet<String> volume2 = dictionary.tailSet("n");


TreeSet
This class implements the Set interface, backed by a TreeMap instance. This class guarantees that the sorted set will be in ascending element order, sorted according to the natural order of the elements (seeComparable), or by the comparator provided at set creation time, depending on which constructor is used.

1:  public class TreeSet<E>  
2:    extends AbstractSet<E>  
3:     implements NavigableSet<E>, Cloneable, Serializable 
 The TreeSet class guarantees that the Map will be in ascending key order and backed by a TreeMap.
 The Map is sorted according to the natural sort method for the key Class, or by the Comparator provided at set creation time, that will depend on which constructor used.
The ordering must be total in order for the Tree to function properly.

Interesting methods
E ceiling(E e)
This method returns the least element in this set greater than or equal to the given element, or null if there is no such element.

List is an ordered collection and can contain duplicate elements. You can access any element from it’s index. List is more like array with dynamic length. List is one of the most used Collection type. ArrayList and LinkedList are implementation classes of List interface.


 

No comments: