How to convert a HashMap to a K/V-String in Java 8 with Streams
Is there any faster, better code to do that? It seems to be costly to append the keys and Values in the map function, doesn’t it?
I would not worry about performance in this case unless it’s a critical part of the system and it’s pointed out as a bottleneck by usage of a profiler or a similar tool. If you haven’t done this before and you think this code is not optimal, then I̶ ̶k̶n̶o̶w̶ ̶t̶h̶a̶t̶ maybe you’re wrong and should test it first.
Please note that in JDK 9 StringJoiner (which is the underlying class for Collectors.joining ) was optimized, thus stream implementation will work about 20-25% faster than in JDK 8 (though still not as fast as naive).
3 Answers 3
map -> map.entrySet().stream().map(Entry::toString).collect(joining(";", "[", "]"))
(Note that I omitted the imports.)
I would not worry about performance in this case unless it’s a critical part of the system and it’s pointed out as a bottleneck by usage of a profiler or a similar tool. If you haven’t done this before and you think this code is not optimal, then I̶ ̶k̶n̶o̶w̶ ̶t̶h̶a̶t̶ maybe you’re wrong and should test it first.
OP’s ‘Collectors.joining()’ call had the delimiter, prefix, suffix misordered. Your solution fixed that.
Note that while Entry.toString() usually returns key = value , it’s not explicitly required by the Entry interface, thus you may hit the map implementation which returns something else. I’d prefer explicit concatenation like in the question.
I have looked at Entry interface. I could not fine any declaration toString . Could you give explain how Entry::toString() define ? Another thing, only Entry::toString can not resolve in my end. I need to do Map.Entry::toString() .
@seal toString() is declared by java.lang.Object , so it’s inherited by everything. If you do import java.util.Map.Entry; , then you can use Entry::toString directly.
Use StringBuilder instead of buffer.
Javadoc => Class StringBuffer
«The StringBuilder class should generally be used in preference to this one, as it supports all of the same operations but it is faster, as it performs no synchronization.» Class StringBuffer
While using StringBuilder may be faster than StringBuffer for the first piece of code, the question is specifically about the speed of the second piece of code, as @LuiggiMendoza points out.
And for readers, the compiler create a StringBuilder in second case for this piece of code: entry.getKey() + » = » + entry.getValue()) on every iteration, so there’s no need to use another StringBuilder . And Collectors#joining will use a StringJoiner that uses a StringBuilder behind the scenes for you, I guess there’s no real improvement using a StringBuilder at all.
Java HashMap content to String [duplicate]
Please read comments before You downmark! I want convert content of HashMap to String as converting ArrayList to String using Arrays.toString(arrayList) . Is it possible ? There is easy way to do this? Do I must have to iteration and appending it using Iterator ? As example:
HashMap> objectMap = new HashMap>(); //. put many items to objectMap Iterator it = objectMap.entrySet().iterator(); while(it.hasNext()) < Map.Entry pairs = (Map.Entry) it.next(); System.out.println(pairs.getKey() + " mt24 mb12">javahashmap