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
Aleksandr M
  • 24,264
  • 12
  • 69
  • 143

3 Answers3

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.

wassgren
  • 18,651
  • 6
  • 63
  • 77
  • it working but showing value in in square brackets. userName/[abcd] password/[pass12] capacity1/[1] capacity2/[10] capacity3/[45] –  Jan 09 '15 at 09:56
  • @Govi, what are you expecting? – wassgren Jan 09 '15 at 09:58
  • It is an array so that it can actually contain more than one value e.g. `capacity/[1, 2, 3]` – wassgren Jan 09 '15 at 09:58
  • 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. – wassgren Jan 09 '15 at 10:01
  • yes at max array will be having one value ,so how should I remove those square brackets. –  Jan 09 '15 at 10:51
  • @Govi - I have updated the answer with more specifics for you. – wassgren Jan 09 '15 at 10:58
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.

Beri
  • 11,470
  • 4
  • 35
  • 57
  • it is showing value in square brackets. userName/[abcd] password/[pass12] capacity1/[1] capacity2/[10] capacity3/[45] –  Jan 09 '15 at 09:57
  • Whell unless you got some external libs attached you can use methods that do join. Otherwise use this solution for printing lists: http://stackoverflow.com/questions/14957964/concantenating-elements-in-an-array-to-a-string . Remember that printing list will always have [] on both sides, because it is a list. – Beri Jan 09 '15 at 10:20
-1

do it this way in for loop

 String s[]=entry.get(key)
 String value = ""+s[0]; //considering you have only one value
Govinda Sakhare
  • 5,009
  • 6
  • 33
  • 74