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
Post a Comment