JavaScript Tips on Objects and Array and their For loop

This is JavaScript Tips on Objects and Array and their For loop.

In JavaScript, there is an Object and represented as:


var someObj = {name:"YongHao", age:25}

There is an Array and represented as:


var someArr = [1,2,3,4]

Use normal for loop only for Array, and use for…in only for Object.

For example, for Object

var someObj = {name:"YongHao", age:25}
for(var i in someObj) {
  console.log(i + ' - ' + someObj[i]);
}

We get:
name – YongHao
age – 25

For Array

var someArr = [1,2,3,4]
for(var i = 0; i < someArr.length; i++) {
  console.log(i + ' - ' + someArr[i]);
}

We get:
0 - 1
1 - 2
2 - 3
3 - 4

Do not use for...in to Array, if you do:

var someArr = [1,2,3,4]
for(var i in someArr) {
  console.log(i + ' - ' + someArr[i]);
}

It may return good result, but not recommended, as i now is a string but not an integer.

Java/JavaScript tips for removing list elements when iterating list

This is Java and JavaScript tips for same topic, removing list elements when iterating list.

In Java, we will use List as an example, as it is uncommon to remove element from an Array. In Javascript we will use Array as an example, as only Array is supported in JavaScript.

Introduction

To remove an element from a Java List, you will do

List someList = new ArrayList();
someList.add("two");
someList.add("one");
someList.add("three");
someList.remove(1); // to remove by index
someList.remove("one); // to remove by element value
System.out.println(someList);

To remove an element from a JavaScript Array, you will do

var someArray = ["two", "one", "three"];
someArray.splice(1, 1); // to remove by index
console.log(someArray);

What if you want to remove an element from a List or Array when you iterating the List, you want to remove it based on some conditions?

In Java, you may do

List someList = new ArrayList();
someList.add("two");
someList.add("one");
someList.add("three");

for(String item : someList) {
  if(item.equals("one")) {
    someList.remove(item); // #1
  }
  else if(item.equals("three")) {
    someList.remove(item); // #2
  }
}

Unfortunately, by doing this, you will get exception thrown.

By executing line #1, “one” is removed from someList, and next if you still continue the for loop, you will get exception as someList is modified, affecting the for loop execution.

The best way to remove item from a Java list is to use Iterator

List someList = new ArrayList();
someList.add("two");
someList.add("one");
someList.add("three");

Iterator it = someList.iterator();
while(it.hasNext()) {
  String item = it.next();
  if(item.equals("one")) {
    it.remove();
  }
  else if(item.equals("three")) {
    it.remove();
  }
}

System.out.println("Latest someList: " + someList);

This is the safe way to remove items from List when you will still continue iterating over the someList.

In JavaScript, you may do

var someArray = ["two", "one", "three"];
for(var i = 0; i < someArray.length; i++) {
  if(someArray[i] == "one") {
    someArray.splice(i, 1); // #1
  }
  else if(someArray[i] == "three") {
    someArray.splice(i, 1); // #2
  }
}

Unfortunately, by doing this, you will get exception thrown.

By executing line #1, "one" is removed from someArray, and next if you still continue the for loop, you will get exception as someArray is modified, affecting the for loop execution.

The best way to remove item from a JavaScript array is to iterate the Array from last.

var someArray = ["two", "one", "three"];
for(var i = someArray.length - 1; i >= 0; i--) {
  if(someArray[i] == "one") {
    someArray.splice(i, 1); // #1
  }
  else if(someArray[i] == "three") {
    someArray.splice(i, 1); // #2
  }
}

This is the safe way to remove items from JavaScript Array.

Why this works? You may think same thing also applies to Java right? Yes you are right. How this works? Leave it for your back home reading.

JavaScript universal http request method – frequest

I wrote some universal JavaScript method for my apps, across Node.JS and Titanium Mobile, Freeswitch, jQuery. Here introduces a very simple but useful method, http request.

We all doing HTTP request on our apps. Make a request via HTTP and get back the response. I designed an API that I can use the same method signature when I am developing apps for Node.JS and Titanium Mobile. Node.JS is a server side program, and Titanium Mobile is a framework compile to Native iOS code. Freeswitch is an open source SIP Media Server, supports JavaScripts to create IVR apps. jQuery is a web based client framework. These platforms had different underlying native libraries to do HTTP request. I just want to design a very simple API that can wrap up these platforms, then I can share code easily across these platform.

The client code I can do:

frequest({
    url : 'http://somewebsite.com',
    callback : function(data) {
        console.log(data);
    }
});

The code for Node.JS:

frequest : function(args) {
		var http = require('http');
		
		try {
			var options = {};
		
			if(args.options) {
				
				options = args.options;
				
			}
			 
			if(args.url) {
				if(args.url.indexOf('http') == -1) return;
				var b = require('url').parse(args.url);
				
				// resolve host name
				if(b.hostname) {
					options.host = b.hostname;
				}
				
				// resolve port
				if(!b.port) {
					b.port = 80;
				}
				
				if(b.port) {
					options.port = b.port;
				}
				
				// resolve web path
				if(b.pathname) {
					options.path = b.pathname;
					
					if(b.search) {
						options.path += b.search;
					}
				}
					
			}
			
			if(args.headers) {
				options.headers = args.headers;
			}
		    
		    var request = http.get(options);
		
			if(args.callback || args.callbackJSON) {
				request.addListener('response', function(response){
				    var data = '';
				
				    response.addListener('data', function(chunk){ 
				        data += chunk; 
				    });
				    response.addListener('end', function(){
				        
				        // prepare data for callback
				        
				        if(data != '') {
				        	if(args.callback) {
					        	args.callback(data);
					        }
					        
					        if(args.callbackJSON) {
					        	try {
					        		var json = JSON.parse(data);
					        		args.callbackJSON(json);	
					        	
					        	} catch (e) {
					        		console.log(e);
					        	}
					        }
				        }
				        
				    });
				});
			}
		} catch (e) {
			console.log(e);
		}
		
		
	
	}

The code for Titanium Mobile:

var frequest = function(args) {
	
	
	var xhr = Ti.Network.createHTTPClient();
	xhr.onload = function() {
		var res = this.responseText;
		
		if(args.callback) {
			args.callback(res);
		}
		
		if(args.callbackJSON) {
			args.callbackJSON(JSON.parse(res));
		}
	};
	
	xhr.onerror = function(e) {
		// detect message
		var errortitle = 'Connection Failure Error';
		var errormsg = ''; // define some suggested network failure message
		if(e.error && e.error.indexOf('A connection failure occurred')) {
			errormsg = 'A connection failure occurred';
		}
		if(args.errorCallback) {
			args.errorCallback({
				e : e,
				errormsg : errormsg
			});
		} else {
			Ti.UI.createAlertDialog({
				title : errortitle,
				message : errormsg
			}).show();
		}
	}
	if(args.timeout) {
		xhr.timeout = args.timeout;
	}
	if(args.progressCallback) {
		xhr.onsendstream = function(e) {
			args.progressCallback(e.progress);
		}
	}
	
	var method = args.method || 'GET';
	xhr.open(method, args.url);
	if(args.headers) {
		for(var k in args.headers) {
			var v = args.headers[k];
			xhr.setRequestHeader(k,v);
		}
	}
	var params = args.params || null;
	if(params != null) 
		xhr.send(params);
	else
		xhr.send();
};

The code for Freeswitch:

frequest : function(args) {
		var result = fetchUrl(args.url);
		if(args.callback) {
			args.callback(result);
		}
	}

The code for jQuery:

var frequest = function(args) {
    $.ajax({
      url : args.url,
      success : args.callback
    });
};

Feel free to share your code for related frequest implementation.

Java tips on joining string with separator

This is Java tips on joining string with separator. Sometimes you have an Array or List and you want to join the String with separator. For example, an Array {“one”,”two”,”three”}, you want to make it as a String as “one,two,three”.

You can have several ways, here do two:

private static String join_1(String[] array, String separator) {
	String s = "";
	String comma = "";
	for(int i = 0; i < array.length; i++) {
		s = s + comma + array[i];
		comma = separator;
	}
	return s;
}

private static String join_2(String[] array, String separator) {
	String s = "";
	for(int i = 0; i < array.length; i++) {
		s = s + array[i] + separator;
	}
	return s.substring(0, s.length() - separator.length());
}

I will recommend first method. Here is my analysis.

Both code run in same speed. Test by putting 10000 elements of array.

First code will simply lesser memory consumption compared to second code. As String#substring will create another new copy of String with different offset.

Java Tips on Array / List / Set / For Each Loop

This is a basic Java Tips on when and how to use Array, List, Set, and for each loop.

When you have a set of data you want to store, you can use Array / List / Set.

Array is a fixed size structure, meant, when you initialise Array to be size of 10, then you cannot alter the size already.

To declare an Array, you type:


String[] someArray = new String[3];
someArray[0] = "one";
someArray[1] = "two";
someArray[2] = "three";

List is a variable size structure, meant, you can add and remove items without declare its size yet.

To declare a List, you type:


List someList = new ArrayList();
someList.add("one");
someList.add("two");
someList.add("three");

You may want to convert Array into List, you do


String[] someArray = new String[3];
List someList = Arrays.asList(someArray);

You may want to convert List back to an Array, you do


List someList = new ArrayList();
String[] someArray = someList.toArray();

Set is an unique, non repeated version of List. For example, using List, you can store [1,2,3,2,1], as 2 repeated 2 times. But, Set, you always cannot repeat the items.

The most common implementation is HashSet, you do


Set someSet = new HashSet();
someSet.add("one");
someSet.add("two");
someSet.add("three");
someSet.add("one");

Note, in the output, you only can find [“one”, “two”, “three”]

And you may not see “one”,”two”,”three” in insertion order. Actually, Java will hash each item, by converting the String into a hash key, then store inside the HashSet, so you may see “two”, “one”, “three” but not “one”, “two”, “three”.

If you want the Set in insertion order, you should use LinkedHashSet.


Set someSet = new LinkedHashSet();
someSet.add("one");
someSet.add("two");
someSet.add("three");
someSet.add("one");

Then you will see “one”, “two”, “three”.

If you want the Set in ordered, you can use TreeSet. Everytime you insert an item, it will auto sort.


Set someSet = new TreeSet();
someSet.add("one");
someSet.add("two");
someSet.add("three");
someSet.add("one");

The output will be “one”, “three”, “two”. Or more better example,


Set someSet = new TreeSet();
someSet.add(3);
someSet.add(2);
someSet.add(1);
someSet.add(3);

The output will be 1,2,3.

To iterate the array, list, set, you can use for each loop construct.

OK, normally, you may already know, something like this


String[] someArray = new String[3];
for(int i = 0; i < someArray.length; i++) { someArray[i] }

You can do this:


String[] someArray = new String[3];
for(String item : someArray) {
item
}

This is same applies to List and Set.

For example:


List someList = new ArrayList();
for(String item : someList) {
item
}

Sometimes, good to use Array, sometimes, good to use List / Set.

If using List and Set, you can do something like add(), remove(), contains().

For example, if you want to filter something, you can use contains(), you can check if the List “contains” the item you want to search.


someList.contains("some thing");

Finally, you can bookmark my blog to follow up more Java Tips posted in future.

Java Tips String comparison

A Java tips during String comparison. We must use .equals() instead of ==.

Using ==, it checks for reference equality.

Using .equals(), it checks for the value equality.

For example,


String a = "YongHao";
String b = "YongHao";
System.out.println(a == b);
System.out.println(a.equals(b));

OK, if you run above code, you will see true true.

But,


String a = new String("YongHao");
String b = new String("YongHao");
System.out.println(a == b);
System.out.println(a.equals(b));

You will get false true.

In first case, a = “YongHao” and b = “YongHao”, as Java will perform String interning by default. String interning meant create a single copy of String and stored in String Constant Pool. So a and b will point to same object in this case, so does a == b is true. String constant pool is fixed size. The reason to create String constant pool is to save space and memory. Note, this is not always guarantee String interning will happens, this is based on JVM setting.

In second case, new String(“YongHao”) guarantee always new object, a and b points to different objects, so a == b return false, but a.equals(b) return true, which is expected.

Conclusion, in Java, when compare String, KEEP IN MIND, always use .equals()

JVisualVM and Eclipse Memory Analyzer a Java Dev Tools

JVisualVM is an open source software to analyze CPU Usage / Memory Usage in real time of a JVM application.

Eclipse Memory Analyzer is an open source software to analyze memory leaks. By passing the Java heap dump this tool will help to pinpoint where is the memory leaks and producing the report which aids developers to fix the bugs.

I am experienced using Eclipse Memory Analyzer along with JVisualVM in a production environment. Last time the production server will went down after running one weeks without any signs. We couldn’t find any clues how the system went down. We then used JVisualVM to monitor the JVM based application server. We got the memory and CPU usage in real time. Then when the server was went down, we quickly open JVisualVM and generate heap dump file. We then passed this heap dump feed into Eclipse Memory Analyzer. After that, Memory Analyzer will tell us where is the memory leaks, which Java object/instances eating up most spaces, then we know how to solve the problem. Finally, after the fix, no more server went down issue.

Additional note, I experienced solved a memory leaks issues on an Apache Tomcat. Technically, in order to enable JVisualVM Monitoring on Apache Tomcat, we need to enable JMX Port in Apache Tomcat conf/server.xml setting. Then open JVisualVM to connect to this JMX Port. Enable JVisualVM Monitoring via JMX on production server is safe as this will not degrade server performance. Same can applies to other JVM based application. This is proven by industry standard. So I strongly recommends.

JVisualVM: http://docs.oracle.com/javase/6/docs/technotes/tools/share/jvisualvm.html

Eclipse Memory Analyzer: http://www.eclipse.org/mat/

Feel free contact me if you need any expertise suggestions.

Java utility to get class version compiled for

Want to check out what is the Java class version compiled for, here comes several ways.

1) javap tool comes along with JDK/JRE
Assume you have a Test.java source code with compiled version of Test.class.
Open your terminal, type
In Unix:
javap -verbose Test | grep "version"
In Windows:
javap -verbose Test | find "version"

Note down Major version number.

Refer to this table:

Major Minor Java Platform Version
45 3 1.0
45 3 1.1
46 0 1.2
47 0 1.3
48 0 1.4
49 0 1.5
50 0 1.6
51 0 1.7
52 0 1.8

2) DataInputStream

There is another clever guy on StackOverFlow just read Java byte class into DataInputStream, and getting the magic number directly yield the minor and major. I copied the code here.

DataInputStream in = new DataInputStream(new FileInputStream(filename));
int magic = in.readInt();
if(magic != 0xcafebabe) {
	System.out.println(filename + " is not a valid class!");;
}
int minor = in.readUnsignedShort();
int major = in.readUnsignedShort();
System.out.println(filename + ": " + major + " . " + minor);
in.close();

3) Unix od
Just below the Java code, other guys found a Unix command line to get the magic number.


od -x HelloWorldJava.class |head -2

I ran this code on my box I found:

od -x Test.class | head -2
0000000      feca    beba    0000    3400    0f00    000a    0003    070c
0000020      0d00    0007    010e    0600    693c    696e    3e74    0001

feca beba is a magic number, 0000 3400 represents Java SE 8.

Check it out my Java version:
java -version

java version "1.8.0_05"
Java(TM) SE Runtime Environment (build 1.8.0_05-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)

What’s use for this check?

I experienced to maintain some legacy code on my box, with my Eclipse set to higher Java JDK version, when deploy to production server it encountered error showing something like “unrecognized class file version”. By using this check I can check the existing java class version on the server, and then go back to my Eclipse and adjust up/down (most probably down) my Java JDK version and compile a new one against it.

Video for using Node.js for Everything

This is another talks video by Charlie Key, the CEO at Modulus, he share with us how Node.JS succeed for development nearly every aspects for a startup where load balance, performance, usability, enterprise class are considered.

He introduced and demo some new tools like Node-inspector, which is a web based inspector to inspect/debug Node code. The debugger interface is nearly same as the native Google Chrome/Safari Developer tools which developed for inspect client web JavaScript and this web based inspector is a completely rewrite mimic the look and feel and features of the native inspector just using JavaScript, and quite powerful, and quite interesting.

Meanwhile he introduced Modulus, an enterprise class Node.JS + MongoDB cloud platform, have a look.

Video for JavaScript & Our Obsession with Speed

This is a talks by Brian Lonsdorf, the CTO of Loop/Recur, who at least last 5 years stuck in JavaScript development, give a talks why we should not always comes to performance in an early stage, and he talks about the benefits of declarative over imperative way.

I spent my time to watch this video and I found useful so I share here to you. Now is Singapore Time 1:15am and I watched this video when I reached home from works just now, but unfortunately I slept in front of computer after this video nearly finished… But I should blog this video before sleep.