Thursday, 27 September 2012

Java basics: converting a collection to a string

If you look up how to write a map or a list out as a string, you can find lots of complicated answers involving loops and even XMLEncoders. It doesn't have to be that hard. The easiest way to convert a Map into a nice string?

String niceString = Arrays.toString(map.entrySet().toArray());

It's even easier for a list:

String niceString = Arrays.toString(list.toArray());


1 comment:

Unknown said...

Hi, Heum, kind of a special Happy new year, but, Mrs, C., you just hacked me with your "Java Performance - Myths, Mysteries, and Paradoxes".

Okay, now I'm literally dying to have the exact spelling of one term:

"special equality" : I'm almost sure it's not the right spelling, cause I've been googling that . It's this notion you speak about: the notion of two objects being referenced on two "close to each other" physical memory location (physical addresses).

pleeeeaaaassssseeee mrs' .. pleeeeease What's the correct spelling...?

I'm so sorry I pop out like this in any post comment's list, it's just that when I wanna learn something, I'm having a hard time letting it go...

Thank you so much for sharing your work in that conference you gave. And I fancied very much the style as well :)

Happy New Year :)