1

Currently have an issue getting a specific object in an arraylist. So I have multiple classes that implements the same interface, and I create objects of the different classes. The problem is that I don't know how to differentiate the classes in the arraylist.

ArrayList<Interface> arraylist = new ArrayList<>();

public static void main(String[] args) {

    addInterface(new interfaceA());
    addInterface(new interfaceB());
    addInterface(new interfaceC());

}

public static void addInterface(Interface foo) {
    arraylist.add(foo);
}

Let say that I want to get interfaceA(), I could call it by arraylist.get(0) but I don't want to hardcode it. Each class has the same methods but the code is different.

8
  • 1
    why do you need this specific object? Commented Apr 13, 2018 at 13:33
  • Lot of problems with the above code: 1. interface is a reserved keyword and can not be used as a variable Interface interface 2. You can not instantiate an interface new interfaceA(). Commented Apr 13, 2018 at 13:33
  • 1
    One, consider a design pattern, such as a Chain of Responsibility and remove the specific attempt to retrieve it; allow each implementation to decide it wants to handle the request or not. If not possible, what are the criteria by which you will select? Perhaps a Map with the appropriate key. Finally, consider looking at this question Type Handler Map which, leaving aside the generics and discussion on Exceptins, shows something similar, which is a registry. Commented Apr 13, 2018 at 13:37
  • If all those classes have overridden equals correct you can just use the methods indexOf or lastIndexOf to find the indexes of specific object Commented Apr 13, 2018 at 13:38
  • 3
    @JayLee that's not what I asked. If you care about this difference in implementation, why are you putting them together in the same list? Commented Apr 13, 2018 at 13:42

3 Answers 3

2

I would use a Map instead of a List. In this case an IdentityHashMap is a good fit.

interface Thing {

}

IdentityHashMap<Class<? extends Thing>, Thing> things = new IdentityHashMap<>();

class ThingA implements Thing {
    @Override
    public String toString() {
        return "ThingA{}";
    }
}

class ThingB implements Thing {
    @Override
    public String toString() {
        return "ThingB{}";
    }
}

class ThingC implements Thing {
    @Override
    public String toString() {
        return "ThingC{}";
    }
}

public void registerThing(Thing thing) {
    things.put(thing.getClass(), thing);
}

public void test(String[] args) {
    registerThing(new ThingA());
    registerThing(new ThingB());
    registerThing(new ThingC());

    System.out.println(things.get(ThingB.class));
}
Sign up to request clarification or add additional context in comments.

Comments

2

You could filter using a predicate, by checking runtime classes:

List<Interface> interfaceAList = arraylist.stream()
                         .filter(e -> InterfaceA.class.isInstance(e))
                         .collect(Collectors.toList());

Comments

0
    public Interface getInterfaceA(List<Interface> interfaces) {
    for (Interface i : interfaces) {
        if (i instanceof InterfaceA)
            return i;
    }
    return null;
    }

1 Comment

Please add some text to describe how your answer would help to solve the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.