简体   繁体   English

将List <Animal>转换为List <Dog>

[英]Casting List<Animal> to List<Dog>

I have an Animal.Class and Dog class which extends Animal.Class 我有一个Animal.Class和Dog类,它扩展了Animal.Class

May I know if there is a quick and easy way to do this? 我可以知道是否有一种快速简便的方法吗?

List<Dog> dogList = getAnimalList();

public List<Animal> getAnimalList(){
     List<Animal> animalList = new LinkedList<Animal>();
     return animalList;
}

I don't wish to look the entire animal List again unless absolutely necessary. 除非绝对必要,否则我不希望再看整个动物名单。

The dog class just contain an extra boolean value for other checking purpose. dog类只包含一个额外的布尔值以用于其他检查目的。

Generics inheritance is little different than java inheritance principle. 泛型继承与java继承原则略有不同。 You need to use ? 你需要用? (wildcards) (通配符)

List<? extends Animal> dogList = getAnimalList();

EDIT: 编辑:

Wildcard Guidelines: 通配符指南:

  1. An "in" variable is defined with an upper bounded wildcard, using the extends keyword. 使用extends关键字定义带有上限通配符的“in”变量。
  2. An "out" variable is defined with a lower bounded wildcard, using the super keyword. 使用super关键字,使用下限通配符定义“out”变量。
  3. In the case where the "in" variable can be accessed using methods defined in the Object class, use an unbounded wildcard. 在可以使用Object类中定义的方法访问“in”变量的情况下,使用无界通配符。
  4. In the case where the code needs to access the variable as both an "in" and an "out" variable, do not use a wildcard. 在代码需要作为“in”和“out”变量访问变量的情况下,不要使用通配符。

Your question isn't completely clear, so I'll just address what might be your issue. 你的问题并不完全清楚,所以我就解决什么可能是您的问题。

If you have a list of animals, which contains many kinds of animals: 如果你有动物名单,其中包含多种动物:

List<Animal> animals = new ArrayList<>();
animals.add(new Dog());
animals.add(new Cat());
animals.add(new Hippo());

And you want to filter the elements of that list down to a specific subtype, then you need to iterate that list and check/cast to the subtype: 并且您希望将该列表的元素过滤到特定的子类型,然后您需要迭代该列表并检查/强制转换为子类型:

List<Dog> dogs = new ArrayList<>();

for (Animal animal : animals) {
    if (animal instanceof Dog) {
        dogs.add((Dog)animal);
    }
}

This is easier done using Guava : 使用Guava更容易完成:

List<Dog> dogs = Lists.newArrayList(Iterables.filter(animals, Dog.class));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 狗是动物,但清单 <Dog> 没有列出 <Animal> 。 如何在泛型/多态函数中安全使用它? - Dog is Animal but list<Dog> is not list<Animal>. How to use it safely in a generic/polymorphic function? 是否可以在没有演员的情况下从 Animal 列表中获取 Dog 列表? - Is it possible to get a list of Dog from a list of Animal without a cast? 是否可以使用类型参数 List 覆盖继承的方法<animal>与列表<dog> ?</dog></animal> - Is it possible to override an inherited method with type parameter List<Animal> with List<Dog>? 是列表<dog>列表的子类<animal> ? 为什么 Java generics 不是隐式多态的?</animal></dog> - Is List<Dog> a subclass of List<Animal>? Why are Java generics not implicitly polymorphic? 类型不匹配:无法从地图转换 <String,List<Animal> &gt;到地图 <String,List<Dog> &gt; - Type mismatch: cannot convert from Map<String,List<Animal>> to Map<String,List<Dog>> 为什么什么时候<t extends animal>选角名单<list<animal> > 列出<list<t> > 导致编译错误,但转换列表<animal>列出<t>引起警告? </t></animal></list<t></list<animal></t> - Why when <T extends Animal> casting List<List<Animal>> to List<List<T>> causes compilation error, but casting List<Animal> to List<T> causes warning? 泛型:列表<? extends Animal>与列表相同<Animal> ? - Generics : List<? extends Animal> is same as List<Animal>? 为什么不能列出<!--? extends Animal-->替换为列表<animal> ?</animal> - Why can't List<? extends Animal> be replaced with List<Animal>? java对象,我用动物speedy = new dog()创建动物或狗吗? 为什么? - java objects, do I create an animal or a dog with animal speedy = new dog(); and why? 通过类调用动物类的方法A - Calling methodA of Animal class by casting
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM