Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

difference between Map<?,?> and Map<Object,Object>

Tags:

java

when I declare a Map or Map <Object,Object> I can put anything in this Map

Map map = new HashMap();
map.put("");

but if I declare it as Map <?,?> I can put nothing in it

Map<?,?> map = new HashMap();
map.put("");

it will goes wrong why ?

like image 664
Wangbo Avatar asked Jun 14 '26 02:06

Wangbo


1 Answers

Map<?,?> map = new HashMap<Integer, Integer>(); // compiles just fine!

? represents some fixed but unknown type. You can't put "" in a Map<Integer, Integer>, and a Map<?, ?> is allowed to be any type of Map, including a Map<Integer, Integer>.

like image 114
Louis Wasserman Avatar answered Jun 15 '26 16:06

Louis Wasserman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!