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;
}