Convert Map to String using Google Guava in Java

This small example shows how java map values can be converted to comma separated string using google guava joiner. This can be used to convert any java collection to a comma or any delimited String.

Other Google Guava examples:
Convert List to Map
Iterables Find Example
Iterables Any Example
Better null checks using Optional Example


package com.idodevjobs;

import com.google.common.base.Joiner;

import java.util.HashMap;
import java.util.Map;

public class GoogleGuavaJoinerExample
{
    public static void main( String[] args )
    {
        Map<String, String> testMap = new HashMap<>();
        testMap.put("key1", "val1");
        testMap.put("key2", "val2");

        System.out.println(Joiner.on(',').join(testMap.values()));
    }
}

Advertisements
This entry was posted in java and tagged , . Bookmark the permalink.

5 Responses to Convert Map to String using Google Guava in Java

  1. Pingback: Convert List to Map using Google Guava in Java | iDoDevJobs

  2. Pingback: Google Guava Iterables Find Example | iDoDevJobs

  3. Pingback: Google Guava Iterables Any Example | iDoDevJobs

  4. Pingback: Google Guava Optional Example | iDoDevJobs

  5. Jeff says:

    Doesn’t this just join and display the Map’s values, without the keys? I’m looking for a useful version of Map.toString() – something that shows me what name/value pairs the Map contains.

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s