8144952: add wildcards to the Map.ofEntries() method
authorsmarks
Fri, 11 Dec 2015 17:01:01 -0800
changeset 34682 fbee345d8615
parent 34572 4edcff1b9a88
child 34683 859915d27f2a
child 34701 0e6ac886b7ad
8144952: add wildcards to the Map.ofEntries() method Reviewed-by: darcy, psandoz, chegar
jdk/src/java.base/share/classes/java/util/Map.java
jdk/test/java/util/Map/MapFactories.java
--- a/jdk/src/java.base/share/classes/java/util/Map.java	Wed Jul 05 21:07:43 2017 +0200
+++ b/jdk/src/java.base/share/classes/java/util/Map.java	Fri Dec 11 17:01:01 2015 -0800
@@ -1670,9 +1670,9 @@
      */
     @SafeVarargs
     @SuppressWarnings("varargs")
-    static <K, V> Map<K, V> ofEntries(Entry<K, V>... entries) {
+    static <K, V> Map<K, V> ofEntries(Entry<? extends K, ? extends V>... entries) {
         Map<K, V> map = new HashMap<>(entries.length * 4 / 3 + 1); // throws NPE if entries is null
-        for (Entry<K, V> e : entries) {
+        for (Entry<? extends K, ? extends V> e : entries) {
             // next line throws NPE if e is null
             map.put(Objects.requireNonNull(e.getKey()), Objects.requireNonNull(e.getValue()));
         }
--- a/jdk/test/java/util/Map/MapFactories.java	Wed Jul 05 21:07:43 2017 +0200
+++ b/jdk/test/java/util/Map/MapFactories.java	Fri Dec 11 17:01:01 2015 -0800
@@ -377,4 +377,13 @@
         assertEquals(sie.toString(), kvh1.toString());
     }
 
+    // compile-time test of wildcards
+    @Test
+    public void entryWildcardTests() {
+        Map.Entry<Integer,Double> e1 = Map.entry(1, 2.0);
+        Map.Entry<Float,Long> e2 = Map.entry(3.0f, 4L);
+        Map<Number,Number> map = Map.ofEntries(e1, e2);
+        assertEquals(map.size(), 2);
+    }
+
 }