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.