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;
}
}
}

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...