Reputation:
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
Upvotes: 0
Views: 434
Reputation: 5749
do it this way in for loop
String s[]=entry.get(key)
String value = ""+s[0]; //considering you have only one value
Upvotes: -1
Reputation: 19211
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.
Upvotes: 3
Reputation: 11610
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.
Upvotes: 2