I want to create a string as quickly as possible from a pair of key values of my HashMap<String, String> m .
I tried:
StringBuffer buf = new StringBuffer(); buf.append("["); for (String key : m.keySet()) { buf.append(key); buf.append("="); buf.append(m.get(key)); buf.append(";"); } buf.append("]");
With Java8, I tried:
m.entrySet().stream() .map(entry -> entry.getKey() + " = " + entry.getValue()) .collect(Collectors.joining("; " , "[" , "]"));
Is there a faster, better code for this? It seems expensive to have keys and values in a map function, right?
source share