Nazila
Nazila

Reputation: 1453

how to convert from a list of generics to list of objects in java 8?

I have this List of generics List<? super Domain>,containing to implementation of Domain: Material and BoM,now I want to get each entity separately.

domainList.stream().filter(a -> a.getClass().equals(BoM.class))
            .collect(Collectors.toList());

with this line i have List<? super Domain> that only contains BoM object.my problem is how to convert this list to List<BoM>?

Upvotes: 4

Views: 1490

Answers (1)

Edwin Dalorzo
Edwin Dalorzo

Reputation: 78579

Well, I'd do it like this:

List<BoM> boms = domainList.stream()
            .filter(BoM.class::isInstance)
            .map(BoM.class::cast)
            .collect(Collectors.toList());

Upvotes: 8

Related Questions