Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type safety: The expression of type Map[] needs unchecked conversion to conform to Map<String,Object>[]

I have the following two lines:

Map<String,Object>[] IEXDivMap = null;       

IEXDivMap =  new Map[IEXJsonArray.length()];

and get the warning:

The expression of type Map[] needs unchecked conversion to conform to Map<String,Object>[]

Is there a way to fix that?

UPDATE:

I was asked in the comments why we need a Map array to begin with. We are getting a series of hashmaps and placing each one inside the map array. Here's the code:

@SuppressWarnings("unchecked")
public static Map<String,Object>[] getDiv(String ticker) {
    Map<String,Object>[] IEXDivMap = null;
    try{
        String url = "https://api.IEXtrading.com/1.0/stock/" + ticker + "/dividends/1y"; 
        URL obj = new URL(url);   

        HttpURLConnection con = (HttpURLConnection) obj.openConnection();       
        con.setRequestMethod("GET");
        int responseCode = con.getResponseCode();
        if(responseCode == 404){
            System.out.println("Ticker " + ticker + " NOT FOUND in getDiv()!");
            return IEXDivMap;                
        }else if (responseCode != 200){
            System.out.println("IEX Printing All Response Header for URL: " + obj.toString() + "\n");
            Map<String, List<String>> map = con.getHeaderFields();            
            for(Map.Entry<String, List<String>> entry : map.entrySet()) {
                System.out.println("IEX " + entry.getKey() + " : " + entry.getValue());
            }
        }

        BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        String inputLine;
        StringBuilder response = new StringBuilder();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);             
        }
        in.close();

        JSONArray IEXJsonArray = new JSONArray(response.toString());        
        IEXDivMap = new Map[IEXJsonArray.length()];

        for (int i = 0; i < IEXJsonArray.length(); i++) {
            IEXDivMap[i] = new HashMap<String,Object>();
            JSONObject IEXJsonObject = IEXJsonArray.getJSONObject(i);

            IEXDivMap[i].put("exDate",IEXJsonObject.getString("exDate"));
            IEXDivMap[i].put("amount",IEXJsonObject.getString("amount"));

            //System.out.println(IEXDivMap[i]);
            System.out.println(IEXDivMap[i].get("exDate") + " 0  " + IEXDivMap[i].get("amount"));
        }
    }catch(Exception e){
        System.out.println("FATAL ERROR: Something went wrong in getDiv " + e.getMessage());
        System.exit(0);
    }
    return IEXDivMap; 
}
like image 239
DCR Avatar asked Dec 29 '18 20:12

DCR


1 Answers

Unfortunately it's not possible to fix the issue in a clean way.

The clean solution would be to create generic array, but it is not possible due to type erasure.

Why generic array creation is forbidden?

Consider the following example with arrays:

Object[] arr = new String[1];
arr[0] = 10;

The result is ArrayStoreException.

Now imagine that generic array creation is allowed:

Map<String, String>[] map = new Map<>[1]; // This is illegal
Object[] objects = map;
objects[0] = new HashMap<Integer, Integer>(); // No ArrayStoreException

The compiler warns that one could put any Map to the array and neither compiler, nor runtime can check it. Hence the warning

Workarounds?

Quoting Java generics faq you can use:

  • array of raw type
  • array of unbounded wildcard parameterized type
  • collection instead of array

Personally I would strongly recommend you to consider using a List:

List<Map<String, Object>> list = new ArrayList<>();
like image 53
Denis Zavedeev Avatar answered Nov 14 '22 22:11

Denis Zavedeev