Java Common Class and Wildcards

I have a problem with generic classes in java.

I have this class:

public abstract class MyMotherClass<C extends AbstractItem> 
{
    private C   item;

    public void setItem(C item)
    {
        this.item = item;
    }

    public C getItem()
    {
        return item;
    }
}

The implementation of this class may be:

public class MyChildClass extends MyMotherClass<ConcreteItem>
{

}

ConcreteItem is just a class that extends AbstractItem (which is abstract).

so MyChildClass has ConcreteItem and I can use:

MyChildClass child = new MyChildClass();
child.setItem(new ConcreteItem());

// automatic cast due to generic class
ConcreteItem item = child.getItem();

Well, at the moment, everything is in order. Here is the problem:

Now I want to extract an instance of MyMotherClass from the collection and set its element (which type is unknown):

Map<String, MyMotherClass> myCollection = new HashMap<String, MyMotherClass>();
Map<String, AbstractItem> myItems = new HashMap<String, AbstractItem>();

// fill the 2 collections
...


MyMotherClass child = myCollection.get("key");
child.setItem(myItems.get("key2"));

If I like it, it works. BUT I have a warning because MyMotherClass is a generic type and I am not using a generic type. But I don't know what type of my extracted child is, so I want to use a wildcard:

Map<String, MyMotherClass<?>> myCollection = new HashMap<String, MyMotherClass<?>>();
Map<String, AbstractItem> myItems = new HashMap<String, AbstractItem>();

// fill the 2 collections
...


MyMotherClass<?> child = myCollection.get("key");
child.setItem(myItems.get("key2"));

: , : setItem ( # 1-of?) MyMotherClass (AbstractItem)

, :

Map<String, MyMotherClass<? extends AbstractItem>> myCollection = new HashMap<String, MyMotherClass<? extends AbstractItem>>();
Map<String, AbstractItem> myItems = new HashMap<String, AbstractItem>();

// fill the 2 collections
...


MyMotherClass<? extends AbstractItem> child = myCollection.get("key");
child.setItem(myItems.get("key2"));

?

, ;)

+3
5

- , MyMotherClass, AbstractItem, C?

public abstract class MyMotherClass<C extends AbstractItem> {

    private AbstractItem item;

    public void setItem(AbstractItem item) {
        this.item = item;
    }

    public AbstractItem getItem() {
        return this.item;
    }

}

:

Map<String, MyMotherClass<?>> myCollection = new HashMap<String, MyMotherClass<?>>();
Map<String, AbstractItem> myItems = new HashMap<String, AbstractItem>();

// fill the 2 collections

MyMotherClass<?> child = myCollection.get("key");
child.setItem(myItems.get("key2"));

.

, MyChildClass MyMotherClass#getItem() :

@Override
public ConcreteItem getItem() {
    return (ConcreteItem) super.getItem();
}

, ; MyMotherClass .

+1

write super.

final Map<String, MyMotherClass<? super AbstractItem>> myCollection =
    new HashMap<String, MyMotherClass<? super AbstractItem>>();

final Map<String, AbstractItem> myItems = 
    new HashMap<String, AbstractItem>();

...

final MyMotherClass<? super AbstractItem> child = 
    myCollection.get("key");
child.setItem(myItems.get("key2"));

extends read.

EDIT: .

, , .

- , , , .

final Map<String, MyChildClass> myInitCollection =
    new HashMap<String, MyChildClass>();

final Map<String, ConcreteItem> myInitItems = 
    new HashMap<String, ConcreteItem>();

myInitCollection.put( "key", new MyChildClass( )  );

final MyMotherClass< ConcreteItem> child = 
    myInitCollection.get("key");

child.setItem(myInitItems.get("key2"));

final Map<String, ? extends MyMotherClass< ? extends AbstractItem >>
    myCollection = myInitCollection;

final Map<String, ? extends AbstractItem> myItems = myInitItems; 

, , myCollection - Map<String, MyMotherClass< ? extends AbstractItem>>.

, .

+5

, , AbstractItem myItems MyMotherClass, myCollection. , ConcreteItem2 MyMotherClass<ConcreteItem1>

- myCollection Map<String, MyMotherClass<AbstractItem>>. AbstractItem , myCollection. ConcreteItem getItem().

, . MyMotherClass<AbstractItem>, AbstractItem, ? super AbstractItem, , , AbstractItem.

, , - Class. , myCollection Class MyMotherClass, , , myItems .

+1

:

Sun ( J2SE 5.0)

:

  • "? extends Type": Type.
  • "? super Type": Type
  • "?":

@alexander-pogrebnyak , super .

0

, .

, ? , , , , .

, , .

, , . .

-2
source

Source: https://habr.com/ru/post/1725737/


All Articles