JDK-8144952 : add wildcards to the Map.ofEntries() method
  • Type: Sub-task
  • Component: core-libs
  • Sub-Component: java.util:collections
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2015-12-08
  • Updated: 2017-05-17
  • Resolved: 2015-12-12
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 9
9 b99Fixed
Description
The Map.ofEntries() method is currently

  static <K, V> Map<K, V> ofEntries(Entry<K, V>... entries)

It should probably be changed to

  static <K, V> Map<K, V> ofEntries(Entry<? extends K, ? extends V>... entries)

For most cases it won't make a difference, but for (probably fairly obscure) cases where you already have Map.Entry instances containing subtypes of the desired Map key/value types, it will be helpful. For example:

    Map.Entry<Integer, Integer> e1 = entry(1, 2);
    Map.Entry<Double, Double> e2 = entry(3.0, 4.0);
    Map<Number, Number> map = Map.ofEntries(e1, e2);

The last line gives an error with the non-wildcard signature. This error doesn't occur if wildcards are added.

It doesn't seem to be a problem if the generic types for the Map.Entry instances are inferred. For example, the following works OK:

    Map<Number, Number> map2 = Map.ofEntries(entry(1, 2.0), entry(3.0f, 4L));