'how to resolve this sonarQube bug about hashmap in java

I have a hashMap : private static Map<String, JSONObject> laserDataList = new HashMap<String, JSONObject>(); I wrote this part of code :

public void execute(JSONObject object) {JSONObject reportData = laserDataList.get(JSONUtil.getAsJSONObject(object, LoanOntologyKeys.PRINT_DOCUMENT_ID)); } Sonar says this code has problem : A "Map<String, JSONObject>" cannot contain a "JSONObject" in a "String" type

how can I resolve this bug ? what is the meaning of this sonar message ?



Solution 1:[1]

I can see from the comments that you're not understanding the problem.

You've defined a Map object. You've specified that the keys of the map are of type String, and the values are of type JSONObject.

When you look up a value in a map, you pass to the "get" method a value of the KEY type, and you get back a value of the VALUE type. In this case, again, the key type is String, and the value type is JSONObject. Your calls to "JSONUtil.getAsJSONObject()" all return JSONObject values, not strings.

I don't know exactly what your logic is doing, but perhaps you need this:

JSONObject reportData = laserDataList.get(LoanOntologyKeys.PRINT_DOCUMENT_ID);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 David M. Karr