coure2011
coure2011

Reputation: 42434

how to retrieve value from Map?

Here is my code

Iterator it = request.getParameterMap().entrySet().iterator();
        while (it.hasNext()) {
            Map.Entry pairs = (Map.Entry)it.next();
            System.out.println(pairs.getKey() + " = " + pairs.getValue());
            it.remove(); // avoids a ConcurrentModificationException
        }

where request is HttpServletRequest. Here is output I am getting in console

id = [Ljava.lang.String;@3f13bf94
title = [Ljava.lang.String;@5eea278c
dlPhone = [Ljava.lang.String;@98a6bd1
name = [Ljava.lang.String;@77736513

why this Ljava.lang.String???? how to get exact value?

Upvotes: 1

Views: 459

Answers (3)

Pavel
Pavel

Reputation: 1647

Try this:

Iterator it = request.getParameterMap().keySet().iterator();
        while (it.hasNext()) {
            String key = (String)it.next();
            System.out.println(key + " = " + requset.getParameter(key));
            //it.remove(); // avoids a ConcurrentModificationException
            // ??? why ???
        }

Upvotes: 0

Saurabh
Saurabh

Reputation: 7964

This is happening because pairs.getValue() is returning String array. Just debug and check that.

You should be iterating over pairs.getValue() and print each entry for your purpose here, or do Arrays.toString( (String[])pairs.getValue() )

Upvotes: 3

Jon Skeet
Jon Skeet

Reputation: 1500475

why this Ljava.lang.String?

Because each value is presumably a String[], and calling toString directly on an array doesn't give anything particularly useful - just the normal implementation inherited from Object, which indicates the type and the system-generated hash code.

To get a useful representation, you could use:

String key = (String) pairs.getKey();
String[] values = (String[]) pairs.getValue();
System.out.println(key + " = " + Arrays.toString(values));

Note that generics would make this much clearer, but if getParameterMap is only declared to return Map, you don't have a lot to go on. You could perform an unchecked cast to at least reduce the amount of code - but it's not actually giving you safety at the point of the cast:

@SuppressWarnings("unchecked")
Map<String, String[]> map = (Map<String, String[]>) request.getParameterMap();
for (Map.Entry<String, String[]> entry : map) {
    System.out.println(entry.getKey() + " = " + Arrays.toString(entry.getValue()));
}

Upvotes: 5

Related Questions