1

In trying to fix a warning about "Type safety: The expression of type List needs unchecked conversion to conform to List". Basically taking a List of Objects and casting it to a List of InventoryPDFAdapter.

This was the code which was throwing the warning

//List<InventoryPDFAdapter> invList = stratificationMap.get(stratification);

This is my solution

List untypedList = stratificationMap.get(stratification);
List<InventoryPDFAdapter> invList = new ArrayList<InventoryPDFAdapter>();
for(Object o : untypedList) {
    invList.add((InventoryPDFAdapter)o);
}

Is there a more elegant way to do this while still avoiding the warning?

  • What is `stratificationMap`? – GBlodgett Sep 08 '19 at 03:42
  • 3
    https://stackoverflow.com/help/minimal-reproducible-example . I'm pretty sure you can just use casting, but it would be easier to help if you made a reproducible example... – Josh Desmond Sep 08 '19 at 03:43
  • If you know for a fact that your list contains only `InventoryPDFAdapter` objects, you may just cast the entire list and insert a `@SuppressWarning` tag. If you're unsure, you should probably issue a custom error message in your loop if one object isn't. – Ole V.V. Sep 08 '19 at 03:56
  • 3
    Please post the declaration of variable `stratificationMap`. – Abra Sep 08 '19 at 04:24
  • 1
    Why do you use `List` without the generics parameter? – Progman Sep 08 '19 at 10:11

0 Answers0