How to convert a HashMap to a K/V-String in Java 8 with Streams -


i want create string of key value pairs of hashmap<string, string> m fast possible.

i tried:

stringbuffer buf = new stringbuffer(); buf.append("["); (string key : m.keyset()) {    buf.append(key);    buf.append("=");    buf.append(m.get(key));    buf.append(";"); } buf.append("]"); 

with java8 tried:

m.entryset().stream()             .map(entry -> entry.getkey() + " = " + entry.getvalue())             .collect(collectors.joining("; " , "[" , "]")); 

is there faster, better code that? seems costly append keys , values in map function, doesn't it?

map -> map.entryset().stream().map(entry::tostring).collect(joining(";", "[", "]")) 

(note omitted imports.)

like luiggi mendoza said:

i not worry performance in case unless it's critical part of system , it's pointed out bottleneck usage of profiler or similar tool. if haven't done before , think code not optimal, i̶ ̶k̶n̶o̶w̶ ̶t̶h̶a̶t̶ maybe you're wrong , should test first.


Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -