3

I have an Animal.Class and Dog class which extends 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.

Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
seesee
  • 1,145
  • 5
  • 20
  • 46
  • 1
    You should't cast the whole list. Cast an object to `Dog` as needed. – KV Prajapati Aug 17 '12 at 03:32
  • You want to make a `List` contain something other than `Dog`s? I feel uneasy, even if it could be done. – Eugene Ryabtsev Aug 17 '12 at 03:33
  • @AVD But the question is: "How to *view* a List as a List since Dog is an Animal?" (*Initially* the ability for such a cast might seem like a valid/sane/accepted/common task ..) And there *are* a bunch of existing questions on this (with the same class names). –  Aug 17 '12 at 03:43
  • For instance (these seems like decent enough questions/answers at a quick glance): http://stackoverflow.com/questions/2346763/any-simple-way-to-explain-why-i-cannot-do-listanimal-animals-new-arraylistd?rq=1 , http://stackoverflow.com/questions/2575363/generics-list-extends-animal-is-same-as-listanimal?rq=1 , http://stackoverflow.com/questions/2745265/is-listdog-a-subclass-of-listanimal-why-a , ,http://stackoverflow.com/questions/7112036/c-sharp-how-to-convert-listdog-to-listanimal-when-dog-is-a-subclass-of-an?rq=1rent-javas-generics-implicit?rq=1 –  Aug 17 '12 at 03:45

2 Answers2

6

Generics inheritance is little different than java inheritance principle. 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.
  2. An "out" variable is defined with a lower bounded wildcard, using the super keyword.
  3. In the case where the "in" variable can be accessed using methods defined in the Object class, use an unbounded wildcard.
  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.
kosa
  • 65,990
  • 13
  • 130
  • 167
1

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:

List<Dog> dogs = Lists.newArrayList(Iterables.filter(animals, Dog.class));
Paul Bellora
  • 54,340
  • 18
  • 130
  • 181