Sunday, 27 September 2015

Serialization

This blog will cover following details.

Introduction
Object graphs in  Serialization
Customized serialization
Serialization wrt inheritance
Externalization
SerialVersionUID

Introduction

   The process of converting an object from java supported form to file/network supported form is called serialization. The process of converting an object from file/network supported form to java supported form is called de-serialization

Java Object is a binary data.

How to implement serialization?
   Create Java Object
   Create Object output stream -> converts Object data to binary data
   File Output stream -> Writes the binary data to file data

import java.io.*;

class Dog implements Serializable{
 int i=10;
 int j=20;
}
 
class SerializeDemo{
  public static void main(String[] args) throws Exception{
  Dog d= new Dog();
  FileOutputStream fos=new FileOutputStream("abc.txt");
  ObjectOutputStream oos = new ObjectOutputStream(fos);
  oos.writeObject(d);
  FileInputStream fis=new FileInputStream("abc.txt");
  ObjectInputStream ois=new ObjectInputStream(fis);
  Dog d2=(Dog)ois.readObject();
  System.out.println(d2.i + ".. " + d2.j);
}
}

Points to Note:
   Serializable interface does not contain any methods. Such interfaces with out any methods are called marker interface.

  To serialize an object, the object must be serializable

transient keyword plays important in serialization. It is a modifier and is applicable only for variables. When a variable is marked as transient, jvm ignores original value and save default value to file
Static:  static variables are at Class level and not part of Object. They do not participate in serialization.
Final: Every final variable will be replaced with actual value at compile time itself.  They will never be available as variable at runtime. They will participate in serialization as value, but will have no impact.

We can serialize any number of objects to a file. De-serialization will happen in the same sequence. If we don't know order of objects in Serialization we can use instanceOf function.

FileInputStream fis=new FileInputStream("abc.txt");
ObjectInputStream ois =new ObjectInputStream(fis);
Object obj=ois.readObject();
  if (obj instanceof Dog) {Dog d2=(Dog) obj; }

Object Graphs in Serialization - When an Object is serialized, all Objects which are reachable from that Object will be serilized automatically.  This group of objects is nothing bug Object Graph.

In Object Graph, every object should be Serializable. If atleast one object is non serializable then we will get Runtime Exception saying NotSerializableException.


class Dog implements Serializable{
  Cat c=new Cat();
}

class Cat implements Serializable{
  Rat r= new Rat();
}

class Rat implements Serializable{
  int j=20;
}

class SerializedDemo{
  public static void main (String[] args) throws Exception{
  Dog d1=new Dog();
 FOS fos=new FOS("abc.txt"):
 OOS oos= new OOS(fos);
 oos.writeObject(d1);

 FIS fos=new FIS("abc.txt"):
 OIS oos= new OIS(fis);
 DOG d2=(Dog)ois.readObject();
sop(d2.c.r.j);

}
}


Customized Serialization: In default serialization there may be loss of information due to transient key word. To recover such loss of information we should go for Customized Serialization.

We can implement customized serialization using 2 methods.
private void writeObject(OOS os) throws Exception
private void readObject(OIS is) throws Exception
 These methods will be executed implicitly by jvm during serialization/de-serialization. These are called as callback methods.
eg:
class Account implements Serializable{
  String un="Venkat";
  transient String pwd="password";
private void writeObject(OOS os) throws Exception{
  os.defaultWriteObject();  //This is meant to ask jvm to perform default Serialization.
  String epwd="()"+pwd;
  os.writeObject();  
}
private void readObject(OIS is) throws Exception{
  is.defaultReadObject();  //This is meant to ask jvm to perform default De-Serialization.
  String epwd=(String)is.readObject();
  pwd=epwd.substring(2);
}
}

Serialization with respect to Inheritance:
Case1: If parent is Serializable then by Default every child is Serializable.
GenericServlet is serializable. All servlet classes in java by default implements Serializable.
Case2:Child class is marked as serializable where as Parent class is NOT. Points to note:

    a)    To Serialize Child Class Object, Parent class Neet not be Serialized.
    b)    At the time of Serialization, jvm will check if any instance variable is inherited from non-Serializable parent. JVM will ignore such variable Original values and save default values to the file.
    c)    At the time of DeSerialization JVM will check if any Parent class is  Non-Serializable. If any Parent class is NonSerializable then JVM will execute instance control flow(identification of instance members) on that non-serializable parent and share its instance variables to the current object.

     d)    To execute instance control flow execution of Non-Serializable parent JVM will always invoke No Argument Constructor. Hence every non-Serializable class should contain No-Argument Constructor. Else system raises run time exception saying InvalidClassException.


Externalization:
   Serialization should be preferred if we want to save complete object. In this case, jvm takes care of complete process.
   Externalization should be preferred if we want to save part of the object to file. In this case programmer has complete control.

Externalizable is child interface of Serializable
Serializable is a marker interface. Where as Externalizable has 2 methods. writeExternal() and readExternal()

writeExternal() -> this method will be executed at the time of serialization
readExternal()-> At the time of deserialization, file does not contain complete object. JVM will create separate new object automatically by calling public no argument constructor. On that object JVM will call readExternal() method to read values from file and assign to the object.


 Serial Version UID
  In serialization both sender and receiver need not be same and need not be from same location and need not use same location. At the time of serialization JVM will save a unique id with every object. This unique id will be generated by jvm based on a .class file. At the time of deserialization Receiver side jvm will compare object unique id with local .class unique id. Deserialization will be performed only when both are matched. Otherwise receiver will fail to deserialize and will get run time exception. The unique id is called  Serial Version UID

Points to note: Problems of depending on default Version UID generated by jvm::

Both sender and receiver should use same jvm wrt vendor and version
After serialization if we change .class file at receiver side then we can't perform deserialization because of mismatch in serialVersionUID.
To generate serialVersionUID, internally jvm will use complex algorithm which may create performance problem.

We can solve above problem by configuring our own serialVersionUID as below
   private static final long serialVersionUID = 1L;


Wednesday, 23 September 2015

Collections

Java Collection Framework is a set of classes and interfaces, which implement the collection data structures like lists, stack, queue or maps.

Collection is a group of individual objects. It is to represent group of individual objects as a single entity.  

Almost all the collections are derived from java.util.Collection interface!!
The collection interface extends java.lang.Iterable interface.



Limitations of Arrays:
  • Fixed in size
  • They can hold only homogeneous elements
  • Arrays are not implemented on some standard data structure, and hence ready made method support not available  
Array vs Collection:
  • Collections are growable, arrays are fixed in size
  • Collections allocates memory on demand basis, arrays preallocate the memory.
  • Collections performance is slow compared to Arrays
  • Collections can hold homogeneous and hetrogenous objects.
  • Underlying data structure not available for Arrays and hence ready made method support not available
  • Collections can hold only Objects but not primitives
Key Interfaces of Collection Framework:
  • Collection(I) - It is to represent group of individual objects as single entity. It defines most of the common methods which are applicable for any collection object
    • Collection vs Collections : Collection is an interface to represent group of individual objects. Collections is a utility class present in java.util package, to define several utility methods like Sorting, Searching  for Collection objects 
    • List(I) - It is child interface of Collection. It is a Collection where duplicates are allowed and insertion order is preserved. Implementation classes are ArrayList, LinkedList, Vector<-Stack
    • Set(I)- It is child interface of Collection. It is a Collection where duplicates are NOT allowed and insertion order is NOT preserved. Implementation classes are Hashset<- LinkedHashSet
      • SortedSet(I)- It is child interface of Set(I). Duplicates NOT allowed, and the objects inserted in some sorting order
        • NavigableSet(I)- It is child interface of SortedSet(I). It defines several metholds for navigation purpose. TreeSet is the implementation class for it.
    • Queue(I) - It is to represent list of individual object prior to processing. PriorityQueue, BlockingQueue are implementation classes
Collections are to represent group of individual objects. If we want to represent objects a Key, Value pairs then we should go for Map interface.
  • Map(I)- Not a child interface of Collection. It is to represent individual objects as key value pairs. Duplicate keys are not allowed, but values can be duplicate
    • HashMap <- LinkedHashMap (Implementation class)
    • WeakHashMap (Implementation class)
    • IdentityHashMap (Implementation class)
    • Hashtable<- Properties (Hashtable is child of Dictionary abstract class as well)
    •  SortedMap(I) - It is child interface of Map(I). It is to represent key, value pairs according to some sorting order of keys
      • NavigableMap(I)- It is child interface of SortedMap(I). It defines several metholds for navigation purpose. TreeMap is the implementation class for it.
Sorting-
  • Comparable(I)  - For default natural sorting
  • Comparator(I) -  For customized sorting
Cursors- To get Objects one at a time from Collection
  • Enumeration(I)  - For default natural sorting
  • Iterator(I) -  For customized sorting
  • ListIterator(I) 
 Utility Classes
  • Collections- Defines several utility methods for Collection Objects
  • Arrays- Defines several utility methods for Array Objects
Collection(I) - It is to represent group of individual objects as single entity. It defines most of the common methods which are applicable for any collection object.

Collections are used to hold and transfer Objects from one place to another place, to provide support for the same, every collection already implements Serializable and Cloneable  interfaces.
 
  •  Metholds
    • boolean add (Object o)
    • boolean addAll(Collection c)
    • boolean remove(Object o)
    • boolean removeAll(Collection c)
    • void clear()
    • boolean retainAll(Collection c)
    • boolean isEmpty()
    • int size()
    • boolean contains(Object o)
    • boolean containsAll(Collection c)
    • Object[] toArray() 
    • Iterator iterator()
Collection interface doesn't contain any method to retrieve objects.

  • List(I) - It is child interface of Collection. It is a Collection where duplicates are allowed and insertion order is preserved. Index plays key role to differentiate duplicates and preserve insertion order. Implementation classes are ArrayList, LinkedList, Vector<-Stack
    •  Metholds
      • All the methods from Collection(I)
      • add add(int index, Object o)
      • boolean addAll(int index, Collection c)
      • Object remove(int index)
      • int indexOf(Object o)
      • int lastIndexOf(Object o)
      • get(int index)
      • ListIterator ListIterator()
      • Object set(int index, Object o)
    •  Implementation Classes 
      • ArrayList
      • LinkedList
      • Vector<-Stack
ArrayList
  • Resizable array or growable one dimensional array is the underlying data structure
  • Random access with O(1) - access items by index list(2)
  • All other operations take O(n) complexity
    • Deletion of items is O(n) as it should shift each item in list
  • Equivalent to Vector but not synchronized
  • Insertion order preserved
  • Hetrogenious objects are allowed (*In Collections, TreeSet and TreeMap doesn't allow heterogeneous objects as they sort the objects by using Comparator for sorting)
  • Accepts 'Null' insertion.
  • 10 is the default initial capacity when created with default constructor. Once the arraylist reaches its max capacity, a bigger arraylist object will be created and copies the values from previous arraylist object. It then reassigns the reference variable to the new object. Note: The new capacity = (current capacity * 3/2)+1
  • Constructors
    • ArrayList l = new ArrayList();
    • ArrayList l = new ArrayList(int initialcapacity);
    • ArrayList l = new ArrayList(Collection c); //inter conversion between collection objects


ArrayList and Vector implements RandomAccess interface, so that any random element can be accessed with same speed. If frequent operation is retrival, ArrayList is the best choice.
ArrayList is the worst choice if frequent operation is insertion or deletion in the middle.


ArrayList vs Vector
  • Vector is synchronized, whereas ArrayList is not. We can convert ArrayList to Synchronized using method Synchronize array listl= Collections.synchronizedList(l1);
  • Vector is thread safe, Arraylist is not
  • Arraylist performs better than Vector as it is not synchronized
  •  
LinkedList
  • It is another implementation of List Interface. It also implements Queue, Deque interfaces as well.
  • Doubly linked list is the underlying data structure
  • Not Synchronized 
  • No sequentional/Random access. Need to iterate to find element and hence it is o(n
  • All other operations(insert, delete) take O(1) complexity
  • Duplicates are allowed
  • Insertion order preserved
  • Hetrogenious objects are allowed (*In Collections, TreeSet and TreeMap doesn't allow heterogeneous objects as they sort the objects by using Comparator for sorting)
  • Accepts 'Null' insertion.
  • LinkedList is usually used to implement Stack and Queues. To provide support for the same LinkedList defined following classes:
    • void addFirst(Object o)
    • void addLast(Object o)
    • Object getFirst()
    • Object getLast()
    • Object removeFirst()
    • Object removeLast()
  • Constructors
    • LinkedList l = new LinkedList();
    • LinkedList l = new LinkedList(Collection c); //inter conversion between collection objects

LinkedList is the best choice when frequent operation is insertion and deletion in the middle. It is worst choice when frequent operation is retrieval.

Vector
  • It is similar to ArrayList with 2 differences
  • Vectors are synchronized
  • Constructors
    • Vector l = new Vector();
    • Vector l = new Vector(int initialcapacity);
    • Vector l = new Vector(int initialcapacity, int incrementalCapacity);
    • Vector l = new Vector(Collection c); //inter conversion between collection objects

Stack
  • It is child class of Vector
  • It is specially designed class for Last In First Out order
  • Constructors
    • Stack s = new Stack();
  • Methods
    • push(Object obj)
    • Object pop() -> to remove and return top object in stack
    • Object peek() -> to return top object with out removal 
    • empty() -> to check if stack is empty or not
    • int search("A");  //offset from top of stack
  • Applications -> Graph traversing with depth-first search
Queue
  • Insert items at the end and remove from begining - FIFO Structure
  • LinkedList, PriorityQueue & ArrayDeque are concrete implementations of Queue interface
  • Applications -> Graph traversing with breadth-first search 
Priority Queue 
  • It is based on priority Heap
  • We assign a priority value to every single item
  • Elements in priority queue are ordered according to their natural order or  
Deque 
  • Doubly ended queue
  • Insertion and removal can happen from both sides of queue
  • Not thread safe
  • It can be used as both queue and stack


Cursors in Java - To retrieve objects one by one from Collection

  • Enumeration
    • We can create enumeration by using elements() method in Vector class
      • Enumeration e=<vector object>.elements();  // to create enumeration
    • Methods in Enumeration
      • boolean hasMoreElements() 
      • Object nextElement()
    • Limitations
      • Applicable for legacy classes and not universal
      • We can only read. It doesn't support remove operation.
  • Iterator
    • Universal cursor, applicable for any collection object
    • Supports both read and remove operations
    • We can create iterator by using iterator() method on any collection object
      • Iterator itr=<any Collection object>.iterator();  // to create iterator
    • Methods in Iterator
      • boolean hasNext() 
      • Object next()
      • void remove()
    • Limitations
      • Single direction cursors
      • replacement not possible
  • ListIterator - Child interface of Iterator
    • Bidirectional cursor
    • Supports read, remove, replace, add operations
    • We can create list iterator by using listIterator() method on any List object
      • ListIterator ltr=<any list object>.iterator();  // to create iterator
    • Methods in ListIterator
      • boolean hasNext() 
      • Object next()
      • void remove() 
      • int nextIndex()
      • boolean hasPrevious() 
      • Object previous()
      • int previousIndex()
      • void set(Object o)
      • void add(Object o)
    • Limitations
      • Applicable only for List implemented class objects. Not a universal cursor.
Collections Class methods(getElements(), iterator(), listIterator() implements Enumeration, Iterator, ListIterator interfaces as an anonymous inner class.  Following program can show the class name that implements the cursor interfaces.


  • Set(I)- It is child interface of Collection. It is collection that contains NO duplicates
    • Implementation classes are Hashset<- LinkedHashSet
      • Underlying data structure is HashMap and O(1) complexity for add(), remove(),contains(),size()
      • Insertion order not preserved. 
      • Hetrogeneous objects allowed
      • NULL insertion allowed
      • Implements Serializable and Cloneable interfaces
      • Best option for SEARCH operations
  • LinkedHashSet
    • Underlying data structure is HashTable + Doubly Linked List
    • Insertion/Iteration order preserved. 
    • O(1) for add, remove & contains()
    • Hetrogeneous objects allowed
    • NULL insertion allowed
    • Implements Serializable and Cloneable interfaces
    • Best option for CACHE based applications as duplicates are not allowed and insertion order must be preserved
    • TreeSet 
      • Underlying data structure is Balanced Tree(red black tree)
      • O(logN) for most of the operations
      • Insertion order NOT preserved. 
      • If we are using default natural sorting order, objects should be Comparable & Homogenous
      • NULL insertion allowed as a first element for empty treeset. Insertion of any other element will fail

    Maps - Store Key Value pairs


    • HashMap
      • Object that maps key to values
      • Duplicates not allowed. One key maps to one value only
      • Order is not guarenteed
      • Allows one NULL key and any number of null values
      • Not synchronized
      • It works with hash function and hashing and looking up a value is O(1)
    • LinkedHashMap
      • There is doubly linked list between all of its entries
      • insertion & Iteration order is preserved
      • Not synchronized
    • TreeMap
      • It is concrete implementation of red-black tree
      • The map is sorted according to the natural order of its key or use comparable for custom objects
      • O(logN)  guaranteed because of balanced tree implementation for get, put and remove operaitons
      • Not Synchronized
    Sorting
    Java uses Quick sort for Primitive types & mergesort for reference types
    • Arrays.sort(<array reference>) to sort the one dimensional arrays
    • Collections.sort(<collection reference>) - to sort collections of primitive types
    • Collections of custom objects
      • Custom Class should implement Comparable Interface
        • Collections.sort(<reference>)
      • Another class implements Comparator Interface
        • Collections.sort(<reference>,another class instance);

    Comparable Interface
    • Available in java.lang package
    • The element we are trying to insert is obj1. Root element will be the obj2. If compareTo() returns -ve, system will insert the element as left child. As right child for +ve value
    • Comparable interface is meant for Default Natural Sorting Order
    Comparator Interface
    • To define customized sorting order
    • available in java.util package
    • has 2 metholds
      • int compare(Object obj1,Object obj2)  //returns -ve if obj1 has to come before obj2
      • boolean equals()  //implementation is optional as equals method implementation in Object class will be available thru inheritance
    Comparable vs Comparator:
    • Comparable for default sorting order, Comparator for custom sorting order
    • All wrapper classes and String class implements Comparable. Comparator for custom sorting.

    Monday, 7 September 2015

    Json - Java Script Object Notation

    •  It is a format used to store and exchange data. 
    • XML and JSON are popular Data Interchange Formats
     
    Json is a simply text format that facilitates reading and writing. It is a widely used data-interchange language because its parsing and its generation is easy for machines.

    1. Set the environment
    Set a proper environment for the compiler, in order to recognize the JSON's classes. If you want to built your project via Maven, you should add the following dependency to your pom.xml:
        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1</version>
        </dependency>

    Otherwise, you have to add the newest version of json-simple-1.1.1.jar in your CLASSPATH.

    Example:
    TestFile.json
    {
      "id": 1,
      "firstname": "Venkat",
      "languages": [
        {
          "lang": "en",
          "knowledge": "proficient"
        },
        {
          "lang": "fr",
          "knowledge": "advanced"
        }
      ],
      "job": {
        "site": "www.javacodegeeks.com",
        "name": "Java Code Geeks"
      }
    }

               FileReader reader = new FileReader(filePath);
               JSONParser jsonParser = new JSONParser();
               JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);

               // get a String from the JSON object
               String firstName = (String) jsonObject.get("firstname");
               System.out.println("The first name is: " + firstName);

               // get a number from the JSON object
               long id =  (long) jsonObject.get("id");
               System.out.println("The id is: " + id);

               // get an array from the JSON object
               JSONArray lang= (JSONArray) jsonObject.get("languages");

               // take the elements of the json array
               for(int i=0; i<lang.size(); i++){
                   System.out.println("The " + i + " element of the array: "+lang.get(i));
               }
               Iterator i = lang.iterator();

               // take each value from the json array separately
               while (i.hasNext()) {
                   JSONObject innerObj = (JSONObject) i.next();
                   System.out.println("language "+ innerObj.get("lang") +
                           " with level " + innerObj.get("knowledge"));
               }

               // handle a structure into the json object
               JSONObject structure = (JSONObject) jsonObject.get("job");
               System.out.println("Into job structure, name: " + structure.get("name"));

    Program works as follows:
    After we create an instance of JSONParser, we create a JSONObject by parsing the FileReader of our .json file. This JSONObject contains a collection of key-value pairs, from which we can get every value of the json file. To retrieve primitive objects, get() method of the JSONObject's instance is called, defining the specified key as an argument. It is important to add the suitable cast to the method. For array types in json file, JSONArray is used that represents an ordered sequence of values. As you can notice in the code, an Iterator should be used in order to take each value of the json array. A structure in the json file, signs the creation of a new JSONObject in order to retrieve the values.You can see the output of the execution below.
    Output:
    The first name is: Venkat
    The id is: 1
    The 0 element of the array: {"knowledge":"proficient","lang":"en"}
    The 1 element of the array: {"knowledge":"advanced","lang":"fr"}
    language en with level proficient
    language fr with level advanced
    Into job structure, name: Java Code Geeks