This does not compile, any suggestion appreciated.
...
List
Compil
Another approach would be using a java 8 stream.
List<Customer> customer = myObjects.stream()
.filter(Customer.class::isInstance)
.map(Customer.class::cast)
.collect(toList());
Depending on what you want to do with the list, you may not even need to cast it to a List<Customer>
. If you only want to add Customer
objects to the list, you could declare it as follows:
...
List<Object> list = getList();
return (List<? super Customer>) list;
This is legal (well, not just legal, but correct - the list is of "some supertype to Customer"), and if you're going to be passing it into a method that will merely be adding objects to the list then the above generic bounds are sufficient for this.
On the other hand, if you want to retrieve objects from the list and have them strongly typed as Customers - then you're out of luck, and rightly so. Because the list is a List<Object>
there's no guarantee that the contents are customers, so you'll have to provide your own casting on retrieval. (Or be really, absolutely, doubly sure that the list will only contain Customers
and use a double-cast from one of the other answers, but do realise that you're completely circumventing the compile-time type-safety you get from generics in this case).
Broadly speaking it's always good to consider the broadest possible generic bounds that would be acceptable when writing a method, doubly so if it's going to be used as a library method. If you're only going to read from a list, use List<? extends T>
instead of List<T>
, for example - this gives your callers much more scope in the arguments they can pass in and means they are less likely to run into avoidable issues similar to the one you're having here.
You can create a new List and add the elements to it:
For example:
List<A> a = getListOfA();
List<Object> newList = new ArrayList<>();
newList.addAll(a);
List<Object[]> testNovedads = crudService.createNativeQuery(
"SELECT ID_NOVEDAD_PK, OBSERVACIONES, ID_SOLICITUD_PAGO_FK FROM DBSEGUIMIENTO.SC_NOVEDADES WHERE ID_NOVEDAD_PK < 2000");
Convertir<TestNovedad> convertir = new Convertir<TestNovedad>();
Collection<TestNovedad> novedads = convertir.toList(testNovedads, TestNovedad.class);
for (TestNovedad testNovedad : novedads) {
System.out.println(testNovedad.toString());
}
public Collection<T> toList(List<Object[]> objects, Class<T> type) {
Gson gson = new Gson();
JSONObject jsonObject = new JSONObject();
Collection<T> collection = new ArrayList<>();
Field[] fields = TestNovedad.class.getDeclaredFields();
for (Object[] object : objects) {
int pos = 0;
for (Field field : fields) {
jsonObject.put(field.getName(), object[pos++]);
}
collection.add(gson.fromJson(jsonObject.toString(), type));
}
return collection;
}
As others have pointed out, you cannot savely cast them, since a List<Object>
isn't a List<Customer>
. What you could do, is to define a view on the list that does in-place type checking. Using Google Collections that would be:
return Lists.transform(list, new Function<Object, Customer>() {
public Customer apply(Object from) {
if (from instanceof Customer) {
return (Customer)from;
}
return null; // or throw an exception, or do something else that makes sense.
}
});
You can't because List<Object>
and List<Customer>
are not in the same inheritance tree.
You could add a new constructor to your List<Customer>
class that takes a List<Object>
and then iterate through the list casting each Object
to a Customer
and adding it to your collection. Be aware that an invalid cast exception can occur if the caller's List<Object>
contains something that isn't a Customer
.
The point of generic lists is to constrain them to certain types. You're trying to take a list that can have anything in it (Orders, Products, etc.) and squeeze it into a list that can only take Customers.