Get current Epoch timestamp

#java:

System.currentTimeMIllis();
Note: the value is in milliseconds. Make sure to divide by 1000 if you need the value in seconds

#php:

time()

#objective-c:

NSDate now = [NSDate date];
NSTimeInterval nowEpochSeconds = [now timeIntervalSince1970];

# perl:

time

# python:

import

int(time.time())

#.NET C#

# MySQL:

SELECT unix_timestamp(now());

# Unix/Linux shell:

$ date +%s

#

Loop through dictionary/map/array

#PHP
foreach($map as $k => $v){
echo “key: {$k}, value:{$v}”;
}

#objective-c
for(id key in myDict){
NSLog(@”key:%@, value:%@”, key, [myDict objectForKey:key]);
}

#python
for key in myDict.iterkeys():
print (“key:{0}, value:{0}”.format(key, myDict[key])

#java
for (String key : map.keySet()) {
System.out.println(“key: ” + key + “, value:” + map.get(key));
}

Singleton Design Pattern in Java

Singleton in Java

Singleton is the design pattern that instantiate the class to one object.

Here is the example in java

public class JavaProject {

	public static void main(String args[]){

		System.out.println("IN main class");

		MYClass a = MYClass.mySingleFactory();
		MYClass b = MYClass.mySingleFactory();

		MYClass c = new MYClass(); //Avoid doing this if the class is to be used as Singleton, because this operation returns a new instance of MyClass

		a.setI(1);
		b.setI(2);
		c.setI(3);

		a.printI();
		b.printI();
		c.printI();
		a.printI();
		MYClass.mySingleFactory().printI(); //another way to call the singleton obj
	}
}

public class MYClass {

	private int i;
	static MYClass inst;

	public static MYClass mySingleFactory(){
		if (inst == null){
			inst = new MYClass();
		}
		return inst;
	}

	public void SingletonTest() {

		System.out.println("SingletonTest constructor called");

	}

	public void printI(){
		System.out.println("Value of I is: " + i);
	}

	public void setI(int i_){
		i = i_;
	}
}

Output:
IN main class
Value of I is: 2
Value of I is: 2
Value of I is: 2
Value of I is: 3
Value of I is: 2