lawonga
lawonga

Reputation: 842

Referencing by an object type Java

Is it possible to reference an object by it's type in Java?

For example, I have this:

private static class Pet {
    private String name;

    public String getName() {
        return name;
    }

    public Pet setName(String name) {
        this.name = name;
        return this;
    }
}

public static class CAT extends Pet{}

public static class DOG extends Pet{}

I put a bunch of pets of CATS and DOGS into a linked list that accepts Pets. I want to find the last index a DOG via:

        private Pet dequeueDog() {
        int locationDog = linkedList.lastIndexOf(DOG);
        return linkedList.remove(locationDog);
    }

Is it possible to do so? To reference an object by the type of object it is?

Upvotes: 1

Views: 90

Answers (3)

mekazu
mekazu

Reputation: 2735

Use instanceof like so.

package com.example;

import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.List;

import org.junit.Test;

public class ExampleTest {

    class Pet {
    }

    class CAT extends Pet {
    }

    class DOG extends Pet {
    }

    private Pet dequeueDog(List<Pet> linkedList) {
        int i = 0;
        Integer foundIndex = null;
        DOG found = null;
        for (Pet pet : linkedList) {
            if (pet instanceof DOG) {
                foundIndex = i;
                found = (DOG) pet;
            }
        }
        if (foundIndex != null) {
            linkedList.remove(foundIndex);
        }
        return found;
    }

    @Test
    public void test() {
        DOG someDog = new DOG();
        CAT someCat = new CAT();
        assertEquals(someDog, dequeueDog(Arrays.asList(someCat, someDog, null, someDog, someCat)));
    }

}

Upvotes: 1

nasukkin
nasukkin

Reputation: 2540

It is possible in Java, but a bit ugly. Here is a method that will do it, given a list and a type that you want to search for.

public static <T> T getLastOfType(List<? super T> list, Class<T> type) {
    Object[] arr = list.toArray();
    for (int i = arr.length - 1; i >= 0; i--) {
        if (arr[i] == null) continue;

        if (type.isAssignableFrom(arr[i].getClass())) {
            return (T) arr[i];
        }
    }
    return null;
}

And here is a little test of it in action: http://pastebin.com/yX1v6L9p (Note that the explicit type parameters aren't strictly needed.)

This works by utilizing generics and the Class method isAssignableFrom to check the types of the objects in the list against the desired type.

Upvotes: 1

Steven Hewitt
Steven Hewitt

Reputation: 302

Assuming you're using Java 8, you can filter out all non-DOGs to find the last dog then search for that:

private static Pet dequeueDog(LinkedList<Pet> linkedList) {

    List<Pet> dogList = linkedList.stream().filter(u -> u.getClass() == DOG.class).collect(Collectors.toList());
    int locationDog = linkedList.lastIndexOf(dogList.get(dogList.size()-1));
    return linkedList.remove(locationDog);
}

Here's an example where I put in two DOGs and three CATs to the list, and receive the second DOG I put into the set. You could extend this to remove the nth DOG by changing what you put into dogList.get().

Upvotes: 1

Related Questions