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.


 

Friday, February 28, 2014

Structural Design Patterns: Proxy Design Pattern



The GoF book says you can use the Proxy design pattern to, “Provide a surrogate or placeholder for another object to control access to it”.
The Proxy design pattern is your best friend when you have a remote object you want to work with and make that object seem local, or when you want to control access to that remote object in some way. 
Proxy means ‘in place of’. In attendance roll call, we give proxy for our friends in college right? ‘Representing’ or ‘in place of’ or ‘on behalf of’ are literal meanings of proxy and that directly explains proxy design pattern.


You need to support resource-hungry objects, and you do not want to instantiate such objects unless and until they are actually requested by the client.

There are various connection techniques you can use in Java, such as RMI, to enable proxies to talk to remote objects. Since all you have to do is send text back and forth from automate proxy to automate server.
Sometimes we need the ability to control the access to an object. For example if we need to use only a few methods of some costly objects we'll initialize those objects when we need them entirely. Until that point we can use some light objects exposing the same interface as the heavy objects. These light objects are called proxies and they will instantiate those heavy objects when they are really need and by then we'll use some light objects instead.

Intent
  • Provide a surrogate or placeholder for another object to control access to it.
  • Use an extra level of indirection to support distributed, controlled, or intelligent access.
  • Add a wrapper and delegation to protect the real component from undue complexity.

There are four common situations in which the Proxy pattern is applicable.

1)      A virtual proxy is a placeholder for "expensive to create" objects. The real object is only created when a client first requests/accesses the object.
e.g. In place of a complex or heavy object use a skeleton representation. When an underlying image is huge in size, just represent it using a virtual proxy object and on demand load the real object.
2)      A remote proxy provides a local representative for an object that resides in a different address space. This is what the "stub" code in RPC and CORBA provides.
E.g Think of an ATM implementation, it will hold proxy objects for bank information that exists in the remote server , Remote Server Object RMI
3)      protective proxy controls access to a sensitive master object. The "surrogate" object checks that the caller has the access permissions required prior to forwarding the request.
4)      Smart Reference – Just we keep a link/reference to the real object a kind of pointer.
A smart proxy interposes additional actions when an object is accessed. Typical uses include:
i)             Counting the number of references to the real object so that it can be freed automatically when there are no more references (aka smart pointer),
ii)            Loading a persistent object into memory when it's first referenced,
iii)           Checking that the real object is locked before it is accessed to ensure that no other object can change it.
 
Common situation: Java Proxy objects
  • Virtual Proxies: delaying the creation and initialization of expensive objects until needed, where the objects are created on demand (For example creating the RealSubject object only when the doSomething method is invoked).
  • Remote Proxies: providing a local representation for an object that is in a different address space. A common example is Java RMI stub objects. The stub object acts as a proxy where invoking methods on the stub would cause the stub to communicate and invoke methods on a remote object (called skeleton) found on a different machine.
  • Protection Proxies: where a proxy controls access to RealSubject methods, by giving access to some objects while denying access to others.
  • Smart References: providing a sophisticated access to certain objects such as tracking the number of references to an object and denying access if a certain number is reached, as well as loading an object from database into memory on demand.
    1:  public interface Animal {  
    2:       public void getSound();  
    3:  }
  Then implement same class with some implementation
1:  public class Lion implements Animal {  
2:       @Override  
3:       public void getSound() {  
4:            System.out.println("Roar");  
5:       }  
6:  }  


Below implemented InvocationHandler
1:  import java.lang.reflect.InvocationHandler;  
2:  import java.lang.reflect.Method;  
3:  public class AnimalInvocationHandler implements InvocationHandler {  
4:        private Object realSubject = null;  
5:       public AnimalInvocationHandler(Object realSubject) {  
6:        this.realSubject = realSubject;  
7:       }  
8:       @Override  
9:       public Object invoke(Object proxy, Method method, Object[] args)  
10:                 throws Throwable {  
11:            Object result = null;  
12:         try {  
13:          result = method.invoke(realSubject, args);  
14:         } catch (Exception ex) {  
15:          ex.printStackTrace();  
16:         }  
17:         return result;  
18:       }  
19:  }  

Test Class
1:  import java.lang.reflect.Proxy;  
2:  public class ProxyExample {  
3:       public static void main(String[] args) {  
4:            Animal realSubject = new Lion();  
5:            Animal proxy = (Animal) Proxy.newProxyInstance(realSubject.getClass()  
6:                .getClassLoader(), realSubject.getClass().getInterfaces(),  
7:                new AnimalInvocationHandler(realSubject));  
8:    proxy.getSound();  
9:       }  
10:  }  

Output :  Roar


Example 2 :  We're going to create a Image interface and concrete classes implementing the Image interface. ProxyImage is a a proxy class to reduce memory footprint of RealImage object loading.ProxyPatternDemo, our demo class will use ProxyImage to get a Image object to load and display as it needs.

1:  public interface Image {  
2:       void display();  
3:  }  

Create concrete classes implementing the same interface.
1:  public class RealImage implements Image {  
2:       private String fileName;  
3:       public RealImage(String fileName){  
4:          this.fileName = fileName;  
5:          loadFromDisk(fileName);  
6:         }  
7:       @Override  
8:         public void display() {  
9:          System.out.println("Displaying " + fileName);  
10:         }  
11:         private void loadFromDisk(String fileName){  
12:          System.out.println("Loading " + fileName);  
13:         }  
14:  }  

Implement Proxy class with same interface . 
1:  public class ProxyImage implements Image {  
2:       private RealImage realImage;  
3:       private String fileName;  
4:       public ProxyImage(String fileName){  
5:          this.fileName = fileName;  
6:         }  
7:        @Override  
8:         public void display() {  
9:          if(realImage == null){  
10:            realImage = new RealImage(fileName);  
11:          }  
12:          realImage.display();  
13:         }  
14:  }  
Use the ProxyImage to get object of RealImage class when required.

1:  public class ProxyPatternDemo {  
2:       public static void main(String[] args) {  
3:          Image image = new ProxyImage("test_10mb.jpg");  
4:          //image will be loaded from disk  
5:          image.display();   
6:          System.out.println("");  
7:          //image will not be loaded from disk  
8:          image.display();        
9:         }  
10:  }  


Notes :
1)       Adapter provides a different interface to its subject. Proxy provides the same interface. Decorator provides an enhanced interface.
2)       Adapter Design Pattern - The adapter implements a different interface to the object it adapts where a proxy implements the same interface as its subject.
3)       Decorator Design Pattern - A decorator implementation can be the same as the proxy however a decorator adds responsibilities to an object while a proxy controls access to it.
     4)   Decorator and Proxy have different purposes but similar structures. Both describe
         how to provide a level of indirection to another object, and the implementations keep a reference to the object to which they forward requests
  • A proxy may hide information about the real object to the client.
  • A proxy may perform optimization like on demand loading.
  • A proxy may do additional house-keeping job like audit tasks.
  • Proxy design pattern is also known as surrogate design pattern.