changeset 13331:83e427bf6451

8144952: add wildcards to the Map.ofEntries() method Reviewed-by: darcy, psandoz, chegar
author smarks
date Fri, 11 Dec 2015 17:01:01 -0800
parents fdd84b2265dd
children db0148cc63a6 003ed63d1f0f
files src/java.base/share/classes/java/util/Map.java test/java/util/Map/MapFactories.java
diffstat 2 files changed, 11 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/share/classes/java/util/Map.java	Fri Dec 11 08:30:50 2015 -0800
+++ b/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/test/java/util/Map/MapFactories.java	Fri Dec 11 08:30:50 2015 -0800
+++ b/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);
+    }
+
 }