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
Type erasure in Java means that all parameterized types are erased at runtime. In your case, a List is just a List at runtime and an Optional 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.