在给定 parents 列表的情况下,检索唯一 class 的 children 列表

Retrieve a list of children of a unique class given a list of parents

所以,假设我有一个 parent 特征 class。然后一堆 children class,像 Dotted、Stripped、Blank,都继承自 Feature。

给定一个 List<Feature> 我想得到那个列表中的所有 objects 点 class.

仅供参考,我首先用 features.add(New Dotted())features.add(New Blank())features.add(New Blank()) 等填充 List<Feature> features...

我试过类似的东西:

public List<Dotted> getAllDotted(List<Feature> features){
    List<Dotted> result = features.stream().filter(o -> o.getClass().equals(Dotted.class)).collect(Collectors.toList());
    return result;
}

但它不起作用,因为 Collector.ToList() 不会将 filter() 的结果转换为 List<Dotted>

你可以这样做:

List<Dotted> d = f.stream().filter(o -> o instanceof Dotted).map(o -> (Dotted) o).collect(Collectors.toList());

虽然可能不是很干净。