0

I have a question regarding type generics and lists. I'm still pretty new to type generics so this maybe a stupid question :D

So... I have an abstract class called "Map" which contains an ArrayList of elements which extend from the class "BaseElement" like so:

private final ArrayList<? extends BaseElement> elements;

In this class I also have a method called "addElement" to add an element to that list, while also returning the element back:

protected <T extends BaseElement> T addElement(T element) {
    elements.add(element);
    return element;
}

I don't know why I can't add this element to the list... The type specified for the "addElement" method is extending BaseElement so the element should also be extending from BaseElement, right?

I also have a "getElement" method which should return an element with it's original type (not BaseElement) from the elements list, which currently looks like so:

public BaseElement getElement(int elementID) {
    return elements.stream()
            .filter(element -> element.getID() == elementID)
            .findFirst()
            .orElse(null);
}

Are type generics even the right thing for the job or should I use another technique?

Here is the code mentioned here in it's entirety, which gives a lot more info to this question: https://gist.github.com/Akjo03/8758429c37332688a78a73aa7317569f

Akjo
  • 13
  • 6
  • Are you getting a compiler error ? and exception when trying to add ? – Mauricio Gracia Gutierrez Aug 15 '21 at 16:05
  • If everything is in a class, maybe you should look into using generics for a class which might help your case: https://www.tutorialspoint.com/java/java_generics.htm. This site contains a section for generic classes – LuckyBandit74 Aug 15 '21 at 16:06
  • If you want an "ArrayList of elements which extend from the class "BaseElement", then you want an `ArrayList`, not `ArrayList extends Element>`. – Louis Wasserman Aug 15 '21 at 16:09
  • @MauricioGraciaGutierrez No it's a syntax error saying that the type is not compatible with each other. – Akjo Aug 15 '21 at 16:22
  • @LuckyBandit74 Will look at that and see if it helps – Akjo Aug 15 '21 at 16:23
  • @LouisWasserman Ohhh... that fixed it :D makes sense... thank you! – Akjo Aug 15 '21 at 16:24
  • @LouisWasserman But how would I go about getting the element back without losing the type? – Akjo Aug 15 '21 at 16:25
  • Do all the elements have the _same_ type that extends `BaseElement`? – Louis Wasserman Aug 15 '21 at 18:05
  • @LouisWasserman There are three classes (`Node`, `Way` and `Relation`), which extend from `BaseElement` and all of them are going into this list through the `addElement` method. Getting them out should be done through the `getElement` method, but they shouldn't lose their type. So for example, that I get a `Node` back out from the `getElement` method and not just a `BaseElement`. – Akjo Aug 15 '21 at 18:22
  • Are all of them in the same `Map`? Or are there different `Map`s for each of those three classes? – Louis Wasserman Aug 15 '21 at 18:33
  • They are all in the same `Map`. But i fear that this might not be possible, right? – Akjo Aug 15 '21 at 18:39
  • it seems to me like you need to post a new question and post the link here. this is not peer programming by using comments – Mauricio Gracia Gutierrez Aug 15 '21 at 19:16
  • 1
    https://stackoverflow.com/questions/68795035/how-do-i-create-a-list-with-multiple-types-that-have-the-same-superclass-withou – Akjo Aug 15 '21 at 20:25

0 Answers0