Browse - Programming Tips - Java: Type safety: Unchecked cast from Object to ListString

Date: 2019oct4
Language: Java

Q.  Java: Type safety: Unchecked cast from Object to List<String>

A.  Some code I inherited had:

private List<String> loadListProperty(String propertyKey) { ... if (propValue instanceof List) { return (List<String>) propValue; // Warning here } return null; }
After checking that the code was reasonable I just added @SuppressWarnings("unchecked")
@SuppressWarnings("unchecked") // Added this private List<String> loadListProperty(String propertyKey) { ... if (propValue instanceof List) { return (List<String>) propValue; // No more warning } return null; }
Not the most insightful code tip, but it works.