changeset 34682:fbee345d8615

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 4edcff1b9a88
children 859915d27f2a 0e6ac886b7ad
files jdk/src/java.base/share/classes/java/util/Map.java jdk/test/java/util/Map/MapFactories.java
diffstat 2 files changed, 11 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- 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);
+    }
+
 }