You can't directly cast List to List because Java generics are invariant. This means that List is not the same as List, even though Customer is a subtype of Object.
To handle this, you can stream over the list and cast each element individually:
List<Object> list = getList(); return list.stream().map(Customer.class::cast).toList(); This works because you're casting each individual element, ensuring type safety at runtime. However, be mindful that if any element in list is not a Customer, it will throw a ClassCastException.