java - Customize the way TreeSet prints Map.Entry values -
i have treeset
containing map.entry<string, double>
values , when try use iterator
iterate on structure , print key-value pairs, standard output looks like:
tolkien=40.25 jkrowling=35.5 obowden=14.0
however use custom format output , replace =
sign ->
like:
tolkien -> 40.25, jkrowling -> 35.5, obowden -> 14.0
this code now:
iterator iterator;. iterator = lib.getsortedbooks().iterator(); while (iterator.hasnext()) { system.out.printf(iterator.next() + " "); }
which best way properly format output?
printing methods in system.out
use tostring()
internally, when call tostring()
same thing see in output. replace equals sign arrow.
system.out.printf(iterator.next().tostring().replace("=", " -> ") + " ");
a more elegant approach (actually right way since map.entry
makes no promise tostring()
format):
map.entry<k, v> entry = iterator.next(); system.out.printf(entry.getkey() + " -> " + entry.getvalue() + " ");
wiki
Comments
Post a Comment