Generics don't work that way. They will not "filter" elements of an existing List. In fact, when you compile this code, the compiler will generate warnings about "unchecked or unsafe operations." This is because you are
misusing generics -- especially with the unchecked conversion:
List<
String> list = arrayList;
Properly used, generics would prevent you from adding a non-String to the List in the first place.
See
Java Tutorial - Generics, and especially note
Type Erasure.