I was going through the newly added existing features introduced in Java-8. One simple feature newly added to String class is quiet appealing for me – that is String Join method.
Example:
String.join(" ", "AZY","BAX"); // returns AZY BAX
For curiosity, I have checked the performance (execution time) of this feature by writing a simple java code
public static void main(String[] args) {
long start = System.nanoTime();
String abc= String.join(" ,"AZY","BAX" … // joining 1000 words of size 3 char;
long diff = System.nanoTime() - start;
System.out.println(" Java 8 String Join " + diff);
start = System.nanoTime();
abc= "AZY"+"BAX"+"CBA"+ … // adding 1000 word of size 3 char;
diff = System.nanoTime() - start;
System.out.println(" Tranditional " + diff);
start = System.nanoTime();
new StringBuilder().append("AZY").append("BAX").appe… // appending 1000 word of size 3 char;
diff = System.nanoTime() - start;
System.out.println(" String Builder Append " + diff);
}
The result is not so exciting for me (time in neno sec)
Java 8 String Join 1340114
Tranditional 59785
String Builder Append 102807
The complexity is of o(n) – in-fact it is (n * Size of individual element length)
Other performance measures (memory etc) I have not measured.
My questions are:
- Is there anything wrong in my measurement (most of the time I believe on the jdk guys)
- What is the intent of adding “join” API to String class
- Is there any performance analysis for Java 8 is available