Tuesday, March 8, 2016

Java HashMap Useful information

HashMap permits null values and the null key.  
The HashMap class is roughly equivalent to Hashtable, except that it is
unsynchronized and permits nulls.
This class makes no guarantees as to the order of the map.
In particular, it does not guarantee that the order will remain constant over time.

HashTable has two important parameters which affects its performance
one is initial capacity and load factor.
Capacity is the number of buckets in the Hash Table.
Initial capacity is the capacity of hash table as the time of creation.

LoadFactor is the a measure of how full the hash table to be allowed before its capacity automatically increased.
Hash table rehashed when the number of entries in to the hash table exceeds the product of load factor and current capacity. Rehashed means internal data struture rebuilt.
So the hash table will have twice the number of buckets.

If many key-value mappings are to be stored in hashmap then creating it with a sufficient large capacity
will allow the mapping to be stored more efficiently than letting it perform automatic rehashing.

If many threads access the HashMap object at a single time then its a good practice to wrapp it using the Collections.synchronizedMap at the creating time.
Map mapObject = Collections.synchronizedMap(new HashMap(...));

Friday, March 4, 2016

FACTORY DESIGN PATTERN EXAMPLE

/*
Creational Patterns - Factory Pattern
Factory of what? Of classes.
In simple words, if we have a super class and n sub-classes,
and based on data provided,
we have to return the object of one of the sub-classes,
we use a factory pattern.
*/
class Person {

public String name;
private String gender;

public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}

public void setGender(String gender) {
this.gender = gender;
}
public String getGender() {
return gender;
}

}


class Male extends Person {
  public Male(String fullName) {
setName(fullName);
setGender("Male");
}
public String toString(){
System.out.println("----Person Detail----- ");
System.out.println("Mr. "+getName());
System.out.println("Name : "+getName());
System.out.println("Gender : "+getGender());
return "";
}
}


class Female extends Person {
  public Female(String fullName) {
setName(fullName);
setGender("Female");
}
public String toString(){
System.out.println("----Person Detail----- ");
System.out.println("Ms. "+getName());
System.out.println("Name : "+getName());
System.out.println("Gender : "+getGender());
return "";
}

}

class DPFactory{
  public static void main(String args[]) {

DPFactory factory = new DPFactory();
Person  person = factory.getPerson(args[0], args[1]);
System.out.println(person);

}
public Person getPerson(String name, String gender) {
if (gender.equals("M")){
return new Male(name);
}else if(gender.equals("F")){
return new Female(name);
}else{
return null;
}
}
}

Wednesday, February 24, 2016

What is Serialization in java ?

Serialization is a type of mechanish or process in java using which an object state can be saved in a stream of bytes and its a platform independent so object serialized in one platform can be deririalize in anothe platform.

UseCase
When an object is converted into Bytes Stream then that object can be 
Serialized object can be Stored into database
Serialized object can be stored/write into file
that object can be send over the network
that object can be stored into memory

In java serializable interface is a marker interface there is no method in that interface.
When you want any class object to be serialized then you need to implement the serializable interface.
whenever any object is being serialized then all the member of the object is serialzed except transient and static variables.

also take a look at when to use Volatile ? and  when to use transient ? in java



What is transient in Java ?

About transient 
transient is a keyword which is used with the member variable of a class to exclude a particular variable from the serialization process (In serialization an object state is saved in bytestream by JVM).

One cannot use the transient keyword with the static variable.

transient variable initialized with the default value during de-serialization
(In Deserialization process object state is recovered by JVM except the static and transient variablers)

also take a look at the what is volatile keyword in java

Advantage
many times as a developer we do not want to serialize some members of object at that one can use the transient keyword and it gives us flexibility in this case as per our requirement.

UseCase
may times some variables value is dependent on other variable, means we can gain value of particular variable from other variables value at that time we can exclude particular variable from being serialization process.
sometimes we use some members of class just for logs purpose those variables also no need to be serialize.

What is volatile keyword in java ?

volatile is a keyword in java it is used to indicate java compiler and java thread that do not cache the value of the variable preceded with the volatile keyword and all time read the volatile variable's value from the main memory. 

So whenever you want read and write operation to be atomic on any variable then declare that variable with volatile keyword.

also take a look at what is transient in java

Tuesday, December 8, 2015

ANDROID layout with fix header footer and center area

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
       android:layout_width="match_parent"
   android:layout_height="match_parent"
   xmlns:ads="http://schemas.android.com/apk/res-auto">
   <RelativeLayout
           android:id="@+id/rl_fixHeader"
   android:layout_width="match_parent"
   android:layout_height="30dp"
   android:layout_alignParentTop="true"
   android:background="#123456"
   android:gravity="center"      
                    android:layout_gravity="center">
   <TextView
       android:id="@+id/fix_header_text"
       android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
       android:text="@string/fix_header_msg"        
       android:textSize="25sp"/>
   </RelativeLayout>
   <RelativeLayout
           android:id="@+id/rl_fix_footer"
   android:layout_width="match_parent"
   android:layout_height="30dp"
   android:layout_alignParentBottom="true"
   android:background="#654321"
   android:gravity="center"      
                    android:layout_gravity="center">
   <TextView
       android:id="@+id/fix_footer_text"
       android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
       android:text="@string/footer_msg"
       android:textColor="#FFFFFF"
       android:gravity="center"
       android:textSize="25sp"/>
   </RelativeLayout>
   <ScrollView
   android:id="@+id/sv_scroll_view"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:layout_above="@+id/rl_fix_footer"
   android:layout_below="@+id/rl_fixHeader"
   android:fillViewport="true">  
<LinearLayout
   android:id="@+id/ll_myScrollLayout"
   android:orientation="vertical"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:background="#FFFFFF"
   android:fillViewport="true">
   <TextView
       android:id="@+id/tvBodytext"
       android:layout_width="wrap_content"
               android:layout_height="wrap_content"
       android:text="@string/body_msg"      
       android:gravity="center"
       android:textSize="25sp"/>
</LinearLayout>
   </ScrollView>
</RelativeLayout>  

Scrum and Scrum master

Scrum  Scrum is a framework which helps a team to work together.  It is like a rugby team (the scrum name comes from rugby game). Scrum enco...