0

I am trying to access my request parameters and it is returning hashcode value.what is the right way to access it.

 public String execute()
{
    Map<String, String[]> requestParams = request.getParameterMap();
    for (Map.Entry<String, String[]> entry : requestParams.entrySet())
    {
        System.out.println(entry.getKey() + "/" + entry.getValue());
    }
   return "success";
}

console output:

 userName/[Ljava.lang.String;@d206ca
 password/[Ljava.lang.String;@bbdd1f
 capacity1/[Ljava.lang.String;@1b249ae
 capacity2/[Ljava.lang.String;@37ced
 capacity3/[Ljava.lang.String;@fec020
2

3 Answers 3

3

That is because the values are of type String[] that must be printed in the following way.

for (Map.Entry<String, String[]> entry : requestParams.entrySet()) {
    System.out.print("Key: " + entry.getKey() + ", values: ");
    for (String val : entry.getValue() {
        System.out.println(entry.getValue());
    }
}

Arrays does not override the toString-method in a nice fashion for printing. Therefore each element in the array must be printed.

A shorter version is to use one of the available utility functions from the Java library. E.g. the Arrays class that offers the following:

for (Map.Entry<String, String[]> entry : requestParams.entrySet()) {
    System.out.print("Key: " + entry.getKey() + ", values: " + Arrays.toString(entry.getValue()));
}

Or, if you are into Java 8 the following code will do it for you:

requestParams.entrySet().stream()
        .map(entry -> entry.getKey() + "/" + Arrays.toString(entry.getValue()))
        .forEach(System.out::println);

Edit: After input from the OP - this is actually what was requested.

Map<String, String[]> requestParams = new HashMap<>();
requestParams.put("username", new String[]{"foo"});

for (Map.Entry<String, String[]> entry : requestParams.entrySet()) {
    System.out.print(entry.getKey() + "/" + entry.getValue()[0]);
}

This is not a recommended approach since the getValue-method always returns an array which may be empty (and then an exception will occur). Furthermore, only one element will be printed but according to the OP it is not the case for the current request.

Sign up to request clarification or add additional context in comments.

6 Comments

it working but showing value in in square brackets. userName/[abcd] password/[pass12] capacity1/[1] capacity2/[10] capacity3/[45]
@Govi, what are you expecting?
It is an array so that it can actually contain more than one value e.g. capacity/[1, 2, 3]
But, if you know that it is only one value you can use System.out.println(e.getValue()[0]);. That will print the first value of the array. However, I would not recommend that approach since the value may actually be an empty array which in that case causes an exception.
yes at max array will be having one value ,so how should I remove those square brackets.
|
2

Try this:

System.out.println(entry.getKey() + "/" + Arrays.toString(entry.getValue()));

entry value is an array, so you need to use Arrays to print it.

2 Comments

it is showing value in square brackets. userName/[abcd] password/[pass12] capacity1/[1] capacity2/[10] capacity3/[45]
Whell unless you got some external libs attached you can use methods that do join. Otherwise use this solution for printing lists: stackoverflow.com/questions/14957964/… . Remember that printing list will always have [] on both sides, because it is a list.
-1

do it this way in for loop

 String s[]=entry.get(key)
 String value = ""+s[0]; //considering you have only one value

1 Comment

This code needs a couple of small corrections to even compile and it does not really provide an answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.