If you have a raw type in Java, you can safely assign/cast this to the same type with an unbounded wildcard. For example a List
can be safely cast to a Li
Yes, it's safe. The generic check is just at compile time.
Type erasure in Java means that all parameterized types are erased at runtime. In your case, a List<Optional>
is just a List
at runtime and an Optional<MyClass>
is just an Optional
at runtime.
When you get a contained class from a List
or an Optional
, Java makes a cast to the parameterized type. So not only is your code safe, it is exactly what the Java compiler does.