user1726508
user1726508

Reputation: 73

how to covert map values into string in Java

i am able to print my Out-put in this format System.out.println(map.get("email"));//this is printing fine but i am unable to print same value after assigning it into a String variable. i tried: String email=(String) map.get("email"); System.out.println("Email--"+email);//But this is not printing
How can i convert map values into string? Please help me.

My full code:

String url = "https://www.googleapis.com/oauth2/v1/userinfo?alt=json&access_token="
              + authResponse.accessToken; 
            final StringBuffer r = new StringBuffer(); 
            final URL u = new URL(url);
            final URLConnection uc = u.openConnection();
            final int end = 1000;
            InputStreamReader isr = null;
            BufferedReader br = null; 
            isr = new InputStreamReader(uc.getInputStream());
            br = new BufferedReader(isr);
            final int chk = 0; 
            String pat = "\"(.*)\": \"(.*)\",";
            Pattern pattern = Pattern.compile(pat);
            Matcher matcher = null;
            Map map = new HashMap();

            while ((url = br.readLine()) != null)
            {
                if ((chk >= 0) && ((chk < end))) {
                    matcher = pattern.matcher(url);
                    if(matcher.find()) {
                        map.put(matcher.group(1), matcher.group(2));
                    }
                    //r.append(url).append('\n');
                }
            }
              System.out.println(map.get("email")); 
              String email=(String) map.get("email"); 
              System.out.println(email);

Upvotes: 1

Views: 11063

Answers (5)

Rohit Jain
Rohit Jain

Reputation: 213261

Always use Generic type when using any collection or Map, unless of course you are using Java version older than 1.5. So, declare your Map as : -

Map<String, String> map = new HashMap<String, String>();

And then you won't need a typecast at all. map.get("email") will give you String type result only.

Upvotes: 5

Kumar Vivek Mitra
Kumar Vivek Mitra

Reputation: 33534

Use toString() or "" with +,

String s = map.get("email").toString();

Or

String s = map.get("email")+"";

- And always prefer using Generics with Collection, so you enter specific type into the collection and get that specific type out of the collection.

Eg:

Map<String, String> map = new HashMap<String, String>();

Upvotes: 1

Subhrajyoti Majumder
Subhrajyoti Majumder

Reputation: 41200

It has been recommended to use type specific Map if you are working on java 1.5+. Your Map deceleration would be Map<String,String> map.

Map<String,String> map = new HashMap<>(); // Diamond operator available in java 7 
String email= map.get("email");


Map<String,String> map = new HashMap<String,String>(); // for java 5+ 
String email= map.get("email");

Upvotes: 0

Abubakkar
Abubakkar

Reputation: 15654

Try this:

String email=map.get("email").toString();

Upvotes: 3

Raghunandan
Raghunandan

Reputation: 133560

http://java.dzone.com/articles/two-ways-convert-java-map. Have a look at this link.Also Converting map values to string array convert Map Values into String Array.

Upvotes: 0

Related Questions