What's the fastest way to concatenate two Strings in Java?
i.e
String ccyPair = ccy1 + ccy2;
I'm using cyPair as a key in a HashMap and it's called in a very tight loop to retrieve values.
When I profile then this is the bottleneck
java.lang.StringBuilder.append(StringBuilder.java:119)
java.lang.StringBuilder.(StringBuilder.java:93)
解决方案
The reason why these routines show up in the benchmark is because that is how the compiler implements your "+" under the covers.
If you really need the concatenated string, you should let the compiler do its magic with the "+". If you all need is a key for map lookup a key class holding both strings with suitable equals and hashMap implementations, might be a good idea as it avoids the copying step.