Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

Tuesday, September 4, 2012

Classical operations on Set

Classical operations on SET
Generally set supports union, intersection and minus operations.

The UNION of two sets is the set of elements which are in either set.
For example: let A = (1,2,3) and let B = (3,4,5). Now the UNION of A and B, written A union B = (1,2,3,4,5). There is no need to list the 3 twice.

The INTERSECTION of two sets is the set of elements which are in both sets.
For example: let A = (1,2,3) and B = (3,4,5). The INTERSECTION of A and B, written A intersection B = (3).

The MiNUS Of two sets is the set elements from A which does not exist in the B.
For example: let A = (1,2,3) and B = (3,4,5). the minus of A and B (A-B) , written A minu B = (1,2)

import java.util.HashSet;

public class SetOperations {
 public static void main(String[] args) {
 
  HashSet<Integer> setA = new HashSet<Integer>();
  HashSet<Integer> setB = new HashSet<Integer>();
 
  setA.add(1);setA.add(2);setA.add(3);
  setB.add(3);setB.add(4);setB.add(5);
 
 
  System.out.println(setA);
  System.out.println(setB);
 
  //UNION
  setA.addAll(setB);
  System.out.println(setA);
 
  /*
  //INTERSECTION
  setA.retainAll(setB);
  System.out.println(setA);
  */
 
  /*
   * //MINUS
  setA.removeAll(setB);
  System.out.println(setA);
  */
 
 
 
 }
}

Monday, September 3, 2012

Singleton Design Pattern


Approach 1: Regular singleton

package

patterns;
public
class Singleton {
private static Singleton myObject = null;
private Singleton() {
}
public static synchronized Singleton getInstance() {
if (myObject== null) {
myObject = new Singleton();
}
return myObject;
}
}


Approach 2: Double Lock check

package
patterns;
public
class SingletonDoubleLock {
private static SingletonDoubleLock myObject = null;
private SingletonDoubleLock() {
}
public static SingletonDoubleLock getInstance() {
if (myObject == null) {
synchronized (SingletonDoubleLock.class) {
if (myObject == null) {
myObject = new SingletonDoubleLock();
}
}
}
return myObject;
}
}


Aproach 3: Early Initilization

package
patterns;
public
class SingletonEarly {
private static SingletonEarly myObject = new SingletonEarly();
private SingletonEarly() {
}
public static SingletonEarly getInstance() {
return myObject;
}
}

Thread Local Example

A simple example for Thread Local concept in Java:

public
class ThreadLocalExamples {
public static int x =0;
private static ThreadLocal<Integer> tlocal;
public static void main(String[] args) {
tlocal = new ThreadLocal<Integer>();
ThreadLocalExamples tl =
new ThreadLocalExamples();
T1 t1 = tl.
new T1();
t1.start();
T2 t2 = tl.
new T2();
t2.start();
tlocal.set(100);
System.
out.println("main thread->"+tlocal.get());
try {
t1.join();
t2.join();
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.
out.println(x);
}
class T1 extends Thread {
@Override
public void run() {
x++;
System.
out.println("value in t1->"+tlocal.get());
tlocal.set(200);
System.
out.println("value in t1->"+tlocal.get());
}
}
class T2 extends Thread {
@Override
public void run() {
x++;
System.
out.println("value in t2->"+ tlocal.get());
tlocal.set(300);
}
}
}

 Output:
main thread->100
value in t1->null
value in t1->200
value in t2->null
2