Friday, 18 March 2016

Array List:



  •  ArrayList works on the principle creating arrays and adding elements to it.
  • Java ArrayList class uses a dynamic array for storing the elements. It extends AbstractList class and implements List interface.
  • Java ArrayList class can contain duplicate elements.
  • Java ArrayList class maintains insertion order.
  • Java ArrayList class is non synchronized.
  • Java ArrayList allows random access because array works at the index basis.
  • In Java ArrayList class, manipulation is slow because a lot of shifting needs to be occurred if any element is removed from the array list.
  • When we create array list in Java, the default size is 10
  • After that when we tried to insert 11th value in array list, then bigger array list will created and copy all value from past array list to new array list and reference will indicate new array list.
  • The size of new array list will be                                                                           New arraylist = (current arraylist*3/2) +1

Example:

List<Integer> l = new ArrayList<Integer>(); //Default size 10
/**
     * Constructs an empty list with an initial capacity of ten.
     */
    public ArrayList() {
        this(10);
    }

l.add(1); //checks for the size .

private void ensureCapacityInternal(int minCapacity) { 
        modCount++; 
        // overflow-conscious code 
        if (minCapacity - elementData.length > 0) 
            grow(minCapacity); 
    } 

Here ensureCapacityInternal method is called when an element is added.
 if (minCapacity - elementData.length > 0) 

In our case 
minCapacity = 1
elementData.length = 10
So minCapacity - elementData.length = 1-10 =-9 which is not greater than ‘ZERO ’ and hence the element is added to the arraylist.

When we insert 11th element into the array,
minCapacity = 11
elementData.length = 10
So minCapacity - elementData.length = 11-10 =1 which is true and hence ‘grow’ method will be called.

private void grow(int minCapacity) { 
        int oldCapacity = elementData.length; 
        int newCapacity = oldCapacity + (oldCapacity >> 1); 
        if (newCapacity - minCapacity < 0) 
            newCapacity = minCapacity; 
        if (newCapacity - MAX_ARRAY_SIZE > 0) 
            newCapacity = hugeCapacity(minCapacity); 
        elementData = Arrays.copyOf(elementData, newCapacity); 
    } 

In our case 
minCapacity = 11
elementData.length = 10

So minCapacity - elementData.length = 11-10 =1 which is greater than zero, hence condition will be true on addition of 11th element and grow method will be called.

Now let’s explore grow method

int oldCapacity = elementData.length; 

In our case
oldCapacity = 10

int newCapacity = oldCapacity + (oldCapacity >> 1); 

In our case
newCapacity =10 + 0.5*10 =15

Here >> is right shift operator which reduces a number to its half

 If (newCapacity - minCapacity < 0) 

In our case
newCapacity - minCapacity = 15-11 =4 which is not less than zero hence next line inside if statement will not be called.
newCapacity = minCapacity; 

 if (newCapacity - MAX_ARRAY_SIZE > 0) 

In our case
newCapacity - MAX_ARRAY_SIZE  = 15 - 2147483639 = -2147483624,which is not greater than zero,hence next line within If statement will not be executed.
newCapacity = hugeCapacity(minCapacity); 

Note : MAX_ARRAY_SIZE is defined in ArrayList class as below
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; 

Next control will reach on below line and this line will create new Array object with capacity 15 using static copyOf method of Arrays class.It will copy the elements of old array to this new array and elementData will start referring to this new Array. Hence there is increase in capacity from 10 to 15 now.
        
elementData = Arrays.copyOf(elementData, newCapacity); 

Next time when 16th element will be added, new size will be calculated as below

15+0.5*15 = 22 and so on.


add(index i, E element)
When an element is inserted into the arraylist at a particular position, which is already filled with some other value, then a new array is created with the index kept vacant and the remaining element shifted to right.
 
List<Integer> l = new ArrayList<Integer>();
l.add(1);
l.add(2);
l.add(1,3);
l.add(4);
 
Here above we are trying to add 3 and position 1, since position 1 already has value '2'. A new array is created with value at index 1 kept vacant and the remaining elements are shifted to right. Than the element 3 is added at index 1.
 
 


get(int index)
The element present at that index in that array is returned. This is very fast.

When to use ArrayList
When the requirement is fetch data frequently and adding data is one time activity.

When not to use ArrayList
When the list is updated frequently

Monday, 14 March 2016

Transformation of JSONX to XML

JSONX to XML Conversion Using Xslt

What is JSON ?

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming LanguageStandard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.
JSON is built on two structures:
  • A collection of name/value pairs. In various languages, this is realized as an object, record, struct, dictionary, hash table, keyed list, or associative array.
  • An ordered list of values. In most languages, this is realized as an array, vector, list, or sequence.
These are universal data structures. Virtually all modern programming languages support them in one form or another. It makes sense that a data format that is interchangeable with programming languages also be based on these structures.

What is JSONx ?

JSONx is an IBM® standard format to represent JSON as XML. The appliance converts JSON messages that are specified as JSON message type to JSONx. The appliance provides a style sheet that you can use to convert JSONx to JSON.
JSONx conversion rules specify how a DataPower® service converts a JSON structure to JSONx (XML).
The DataPower appliance provides the default jsonx2json.xsl style sheet for converting JSONx to JSON. This style sheet is in thestore: directory.

Transformation of Jsonx to Xml
JSONX

<?xml version="1.0" encoding="UTF-8"?>
<json:object xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx">
    <json:string name="name">John Smith</json:string>
    <json:object name="address">
        <json:string name="streetAddress">21 2nd Street</json:string>
        <json:string name="city">New York</json:string>
        <json:string name="state">NY</json:string>
        <json:number name="postalCode">10021</json:number>
    </json:object>
    <json:array name="phoneNumbers">
        <json:string>212 555-1111</json:string>
        <json:string>212 555-2222</json:string>
    </json:array>
    <json:null name="additionalInfo" />
    <json:boolean name="remote">false</json:boolean>
    <json:number name="height">62.4</json:number>
    <json:string name="ficoScore">&gt; 640</json:string>
</json:object>
XSLT

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:json="http://www.ibm.com/xmlns/prod/2009/jsonx" 
exclude-result-prefixes=" json">

<xsl:template match="/">
   <xsl:apply-templates/>
</xsl:template>

 <xsl:template match="json:object[@name]">
    <xsl:element name="{@name}">
       <xsl:apply-templates/>
    </xsl:element>
 </xsl:template>

 <xsl:template match="json:array[@name]">
    <xsl:element name="{@name}">
       <xsl:apply-templates/>
    </xsl:element>
 </xsl:template>

 <xsl:template match="json:string[@name]">
    <xsl:element name="{@name}">
       <xsl:value-of select="."/>
    </xsl:element>
 </xsl:template>

 <xsl:template match="json:number[@name]">
    <xsl:element name="{@name}">
       <xsl:value-of select="."/>
    </xsl:element>
 </xsl:template>

 <xsl:template match="json:boolean[@name]">
    <xsl:element name="{@name}">
       <xsl:value-of select="."/>
    </xsl:element>
 </xsl:template>

 <xsl:template match="json:null[@name]">
    <xsl:element name="{@name}">
       <xsl:value-of select="."/>
    </xsl:element>
 </xsl:template>

</xsl:stylesheet>



Output

<?xml version="1.0" encoding="UTF-8"?>
    <name>John Smith</name>
    <address>
        <streetAddress>21 2nd Street</streetAddress>
        <city>New York</city>
        <state>NY</state>
        <postalCode>10021</postalCode>
    </address>
    <phoneNumbers>
        212 555-1111
        212 555-2222
    </phoneNumbers>
    <additionalInfo/>
    <remote>false</remote>
    <height>62.4</height>
    <ficoScore>&gt; 640</ficoScore>




Tuesday, 15 April 2014

What happens when you run java program?

Step 1 : Initially JVM looks for main() method in your java application. but as you know that 
              static Variables are initialized first.
Step 2 : Static blocks are get invoked.
Step 3 : Main method is invoked.
Step 4 : Constructor will be called when you create an object for the class.
Step 5 : Every default constructor has super(); as a first line. It calls its super class constructor.
Step 6 : If a program contain any Initialization block, then it is copied into the constructor 
             after super(); .
Step 7 : Statements are in the constructor will be executed.
Step 8 :User methods will be called using object of that class.

Sample Program :

program






















output
Output