-3

I have a method, where I am trying to modify the value of one key of a map.

I cannot change the signature of the map .

public void testMethod(Map<String, ?> settings) {

    this.xyzkey = (String) settings.get(xyzkey);

    settings.put("xyzkey" ,settings.get(xyzkey)+"test"));

}

Now when I want to get a specific key from the map and modify its values I am getting Wrong 2nd argument type. Found:'java.lang.String',required:'?'.

Please suggest any way o update the key's value .

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588

2 Answers2

0

You try get the value by the key xyzkey (your class' member) and put the changed value to the key "xyzkey" (a string). So you need to change your get to get("xyzkey") or your put to put(xyzkey, ...).

Stefan Warminski
  • 1,845
  • 1
  • 9
  • 18
0

public void testMethod(Map<String, ?> settings) {

You are using generics wildcards here. In this method your input type for argument is not defined, so you can add anything to the map(except null).

Found well described answer here.

Community
  • 1
  • 1
codingenious
  • 8,385
  • 12
  • 60
  • 90
  • @StefanWarminski while the code didn´t have it, the question title somewhat implies it was just left out here (as the title did have it). – SomeJavaGuy May 17 '17 at 08:44
  • Yeah, just checked my edit. But I just selected the code and used CTRL+k for code formatting. Didn't change anything else. – codingenious May 17 '17 at 08:44
  • @StefanWarminski I asked the question(how generics was added) on meta and people on SO have answer to everything. https://meta.stackoverflow.com/questions/349298/how-does-code-formatting-works – codingenious May 17 '17 at 08:58