Unchecked cast from Object to Map

时间:2020-01-13 10:41:09 类型:JAVA
字号:    

List<HashMap<String,Object>> result = (List<HashMap<String,Object>>)request.getAttribute("result");

写这段代码得到了Unchecked cast from Object toHashMap

出现这个警告是因为进行强制转换的Object的类型没有经过确认,所以这个类型转换时不安全的。如果你确认Object的类型与

HashMap的类型一致的话,可以加上在前面加上@SuppressWarnings("unchecked")来除掉这个警告

 <%
        @SuppressWarnings("unchecked")
     	List<HashMap<String,Object>> result = (List<HashMap<String,Object>>)request.getAttribute("result");
        for(Map<String,Object>row:result){ %>
        	 <tr>
        	      <td><%out.println(row.get("id")); %></td>
	              <td><%out.println(row.get("name")); %></td>
		      <td><%out.println(row.get("sex")); %></td>
		      <td><%out.println(row.get("blood")); %></td>
		      <td><%out.println(row.get("hobby")); %></td>
		      <td><%out.println(row.get("date")); %></td>
		      <td><%out.println(row.get("pic")); %></td>
             </tr>
       <% } %>