Java/JavaScript Tips on Append to File

This is Java/JavaScript Tips on Append content to file. I will introduce append file via command line, Java, and Node.JS (JavaScript).

To append file via command line, in Windows or Unix, you can do:

echo "test" >> a.txt

The file a.txt will be created if not exist at the first time. And the subsequence calls of >> will append the content to next line of the end of file.

To append file via Java, you can do:

PrintWriter pw = new PrintWriter(new FileWriter(new File("a.txt"), true)); 
pw.println("test");
pw.close();

The file a.txt will be created if not exist at the first time. And the subsequence calls of PrintWriter.println will append the content to next line of the end of file.

The interesting thing is I like to use PrintWriter, to call println, which I not need to append \n myself.

Notice the true variable at new FileWriter, this let Java know you want to append file, if you not provide this variable, Java will always overwrite the file but not appending.

To append file via JavaScript, you can do:

in asynchronous calls:

var fs = require('fs');
fs.appendFile('a.txt', 'content\n', function(err) {
 // some callback after append file
});

OR

in synchronize calls:

var fs = require('fs');
fs.appendFileSync('a.txt', 'content\n');

The file a.txt will be created if not exist at the first time. And the subsequence calls of appendFile or appendFileSync will append the content to next line of the end of file.

Node.JS provides asynchronous and synchronize versions for each File IO Calls. Refer the link here. Other than appendFile, for example, readFile/readFileSync, writeFile/writeFileSync.

Node.JS itself is a single threaded and asynchronous. It is always recommended to use asynchronous version of method instead of synchronize version, as synchronize version is a blocking calls. If the content need append to file is very very big, the calls will just hang there, Node.JS cannot handle other requests anymore. To learn how Node.JS back end works, refer here.

Java tips String concatenations using StringBuffer

Java provides StringBuffer and String. Always use StringBuffer when you want concatenate string. StringBuffer has better performance over String when doing simple string concatenation.

String is immutable, that something cannot be changed. When you concatenate using String, JVM actually create new Object for each concatenation. Create new Object is expensive. See below example.

When using String, you may doing something like:

String message = ""; // new object
message = message + "I am "; // another new
message = message + "25"; // another new
message = message + " years old"; // another new

When using StringBuffer, you will doing something like

StringBuffer sb = new StringBuffer(); // new object here
sb.append("I am ");
sb.append("25");
sb.append(" years old");
String message = sb.toString(); // another new

As you can see, by using String, you create a bunch of new objects. When you have more things to concatenate, more and more objects created, and again, creating new object is very expensive, and make application performance worse.

It is strongly recommended by using StringBuffer to append string, and at the last, call toString() to export the string content into String object, then you can use it for any purpose.

So for the example on Java tips on joining string with separator, the code can be changed to below:

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

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

Another note, when use String for concatenation, internally it will create StringBuffer to do actual concatenation. Refer this link for more information.

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.

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.

Trying on Java 8 Stream Parallel

I had been trying on Java 8 Stream Parallel.

Refer following code:

import java.util.*;
public class Test {
	public static void main(String args[]) {
		List list = new ArrayList<>();
		list.add(new Employee("First", "YongHao"));
		list.add(new Employee("Second", "YongHao"));
		list.add(new Employee("Third", "YongHao"));
		
		list.stream().sorted((e1, e2) -> e1.getFirstName().compareTo(e2.getFirstName()))
		        .forEach(System.out::println);
		
		System.out.println("--------------");
		
		list.stream().parallel().sorted(Comparator.comparing(e -> e.getFirstName()))
		        .forEachOrdered(System.out::println);
		        
		        
	}
	
	public static class Employee {
		String firstName;
		String lastName;
		public Employee(String firstName, String lastName) {
			this.firstName = firstName;
			this.lastName = lastName;
		}
		public String getFirstName() {
			return firstName;
		}
		public String getLastName() {
			return lastName;
		}
		public String toString() {
			return firstName + " " + lastName;
		}
	}
}

The result will be:
➜ java2 javac Test.java
➜ java2 java Test
First YongHao
Second YongHao
Third YongHao
————–
First YongHao
Second YongHao
Third YongHao

Look at first:

list.stream().sorted((e1, e2) -> e1.getFirstName().compareTo(e2.getFirstName()))
.forEach(System.out::println);

It first returns the list as a stream, sort the list using the lambda expression and then iterating the list and print its output.

Look at second:

list.stream().parallel().sorted(Comparator.comparing(e -> e.getFirstName()))
.forEachOrdered(System.out::println);

Notice, after returned stream-ed list, there is a chained parallel(), which will actually doing background task distribution fully utilizing your system cores. And notice, forEachOrdered method at the end, which guarantees the ordered result be returned. If we replace forEachOrdered to forEach, you will see random order be returned. Because forEach method cannot guarantee ordered result when doing parallel operation.

Another note, see the chained operation, from the list, to stream(), parallel(), sorted(), at the point Java had not yet execute anything, list.stream() did return a stream-ed list, or more formally, a lazy list, the actual execution started when we call forEach or forEachOrdered function.

I did some simple benchmark to measure the speed for these two, one without parallel and one with parallel.

Here come modified code:

import java.util.*;
public class Test {
	public static void main(String args[]) {
		List list = new ArrayList<>();
		generateList(list);
		long start = System.currentTimeMillis();
		list.stream().sorted((e1, e2) -> e1.getFirstName().compareTo(e2.getFirstName()))
		        .toArray();
		System.out.println("Elapsed:"+(System.currentTimeMillis() - start));
		System.out.println("--------------");
		start = System.currentTimeMillis();
		list.stream().parallel().sorted(Comparator.comparing(e -> e.getFirstName()))
		        .toArray();
		System.out.println("Elapsed:"+(System.currentTimeMillis() - start));        
	}
	
	private static void generateList(List list) {
		for(int i = 0; i < 2000000; i++) {
			Employee e = new Employee("EMP" + i, "YONGHAO");
			list.add(e);
		}
	}
	
	public static class Employee {
		String firstName;
		String lastName;
		public Employee(String firstName, String lastName) {
			this.firstName = firstName;
			this.lastName = lastName;
		}
		public String getFirstName() {
			return firstName;
		}
		public String getLastName() {
			return lastName;
		}
		public String toString() {
			return firstName + " " + lastName;
		}
	}
}

The result will be:
java2 javac Test.java
➜ java2 java Test
Elapsed:1901
--------------
Elapsed:337
➜ java2 java Test
Elapsed:296
--------------
Elapsed:494
➜ java2
➜ java2 java Test
Elapsed:288
--------------
Elapsed:1425
➜ java2
➜ java2 java Test
Elapsed:1834
--------------
Elapsed:422

OK, the result showed that... well, most of the time parallel operation faster, actually it should be faster when operating quite big data. Because it distributes the tasks to multiple cores.

OK, conclusion, worth to learn this new technology.

Stream and Lambda is the new features added in Java 8. A lot more to explore.