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:
The java.lang.string.join () method concatenates the given element with the delimiter and returns the concatenated string. Note that if an element is null, then null is added. The join () method was introduced in java string since JDK 1.8.
The join () method was introduced in java string since JDK 1.8. String.join () method takes two parameters first is a delimiter and the second can be a list or array of elements or elements separated by , (comma). String joined=String.join ("delimiter","element1","element2,"element3"...);
Bookmark this question. Show activity on this post. 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. For curiosity, I have checked the performance (execution time) of this feature by writing a simple java code
The performance benefit of using the final keyword is a very popular debate topic among Java developers. Depending on where we apply it, the final keyword can have a different purpose and different performance implications.
First things first. This is not how you microbench Java
Read How do I write a correct micro-benchmark in Java? first. Your numbers are completely irrelevant, so lets ignore them.
Looking at the second example:
abc= "AZY"+"BAX"+"CBA"+...
These look like compile time constants to me. This String
would be concatenated at compile time and there would be nothing to benchmark. This is a useless comparison as the whole point of the StringBuilder
or String.join
is to concatenate String
s that are not compile time constant.
Moving onto comparing the StringBuilder
and String.join
. Looking at the source code:
public static String join(CharSequence delimiter, CharSequence... elements) {
Objects.requireNonNull(delimiter);
Objects.requireNonNull(elements);
// Number of elements not likely worth Arrays.stream overhead.
StringJoiner joiner = new StringJoiner(delimiter);
for (CharSequence cs: elements) {
joiner.add(cs);
}
return joiner.toString();
}
This uses a StringJoiner
. A StringJoiner
simply uses a StringBuilder
under the hood, so the two are equivalent.
It is often much more informative to look at the code than to try and benchmark performance. Even if you do benchmark correctly.
It's also worth noting that your first method, with join
, joins the 1000 String
s on " " (space). Whereas your StringBuilder
method simply appends them together. These two are not the same.
The point of the String.join
method is that you can do:
String.join(", ", "a", "b", "c") // result is "a, b, c"
With a StringBuilder
you would have to add a lot more code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With