1

As per my understanding(which needs correction obviously) the map should take Integer class and all of its sub classes. and same with Location class.

Map<? extends Integer, ? extends Location> test2 = new HashMap<>();
test2.put(new Integer(5), new Location(1, "Test2", exits));

I have watched and read from many different resources. But I still can't get my head around this. I am not a professional programmer.

Turing85
  • 18,217
  • 7
  • 33
  • 58
gdogra
  • 130
  • 2
  • 12
  • 1
    A `List extends Fruit>` could be a List, or a List, or a List. the point is: you don't know. And since you don't know, you can't add anything to it because adding a Banana to a List would be wrong. So a List extends Fruit> is only useful when all you care about is that you want to **read** from the list, and have the guarantee that all the elements are fruits. – JB Nizet Jul 27 '19 at 12:23
  • 1
    `? extends Integer` means "some unknown, *specific* subclass of `Integer`. – MC Emperor Jul 27 '19 at 12:23
  • Further to @JBNizet’s comment, I can’t stress highly enough : if you are learning generics and/or OOP principles, do NOT try to learn using classes like Integer or Number, nor anaemic names like classA, etc. people run into so much confusion with those. Far FAR better is to follow the example in the comment and learn creating real-world types like Fruit, Animals, etc. – racraman Jul 31 '19 at 04:03

2 Answers2

0

Quoting Oracle Docs for generics

You can use an upper bounded wildcard to relax the restrictions on a variable. For example, say you want to write a method that works on List<Integer>, List<Double>, and List<Number>; you can achieve this by using an upper bounded wildcard.

and

The upper bounded wildcard, <? extends Foo>, where Foo is any type, matches Foo and any subtype of Foo. The process method can access the list elements as type Foo:

So basically test2 is a Map which takes the key as Integer or any class which has extended Integer(i.e subclasses of Integer) and the value as Location or its subclass

Ashvin Sharma
  • 563
  • 1
  • 5
  • 24
0

This can be explained better with small example, let's take a method with generic argument of Map

public static void m1(Map<? extends Number, ? extends Object> test2) {

}

Now you can create Map objects with key as Number or it's child classes and value as Object or it child classes and call that method

    Map<Integer, String> map1 = new HashMap<Integer, String>();
    m1(map1);

    Map<Double, String> map2 = new HashMap<Double, String>();
    m1(map2);

    Map<Number, StringBuffer> map3 = new HashMap<Number, StringBuffer>();
    m1(map3);

    Map<Integer, StringBuilder> map4 = new HashMap<Integer, StringBuilder>();
    m1(map4);

? extends Number doesn't mean map can contains Number or it's child object, it means either you can create Map with key as Number or it's child's. which is the same for value also ? extends Object either Object or it's child's

Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98