Java Tutorials - Herong's Tutorial Examples - Version 7.03, by Dr. Herong Yang
Wildcard Parameterized Subtyping Example
This section provides a tutorial example on wildcard parameterized subtyping using the putAll() method in the java.util.Hashtable class.
Let's see how wildcard parameterized types are used on JDK generic classes. Here is good example in the generic class, java.util.HashMap, as defined in JDK 1.7:
package java.util; class Hashtable<K,V> { ... void putAll(Map<? extends K,? extends V> t) {} ... }
My understanding on putAll() definition is:
The tutorial example below has no compilation errors, because objects provided in the putAll() meet the type requirements:
/* HashtablePutAll.java - Copyright (c) 2014, HerongYang.com, All Rights Reserved. */ import java.util.*; import java.awt.*; import javax.swing.*; class HashtablePutAll { public static void main(String[] a) { // Hashtable API: // Class Hashtable<K,V> { // void putAll(Map<? extends K,? extends V> t) {} // } Hashtable<Number,Container> hashNumberContainer = new Hashtable<Number,Container>(); // allowed - Integer extends Number & Box extends Container HashMap<Integer,Box> mapIntegerBox = new HashMap<Integer,Box>(); mapIntegerBox.put(new Integer(777), new Box(BoxLayout.X_AXIS)); hashNumberContainer.putAll(mapIntegerBox); System.out.println("# of entries: "+mapIntegerBox.size()); // not allowed - Component does not extend from Container // HashMap<Number,Component> mapNumberComponent // = new HashMap<Number,Component>(); // hashNumberContainer.putAll(mapNumberComponent); } }
But if you uncomment the not allowed lines in the example, you will get compilation errors:
HashtablePutAll.java:26: error: no suitable method found for putAll(HashMap<Number,Component>) hashNumberContainer.putAll(mapNumberComponent); ^ method Map.putAll(Map<? extends Number,? extends Container>) is not applicable (argument mismatch; HashMap<Number,Component> cannot be converted to Map<? extends Number,? extends Container>) method Hashtable.putAll(Map<? extends Number,? extends Container>) is not applicable (argument mismatch; HashMap<Number,Component> cannot be converted to Map<? extends Number,? extends Container>) 1 error
Last update: 2014.
Table of Contents
Execution Process, Entry Point, Input and Output
Primitive Data Types and Literals
Bits, Bytes, Bitwise and Shift Operations
Managing Bit Strings in Byte Arrays
Reference Data Types and Variables
StringBuffer - The String Buffer Class
System Properties and Runtime Object Methods
►Generic Classes and Parameterized Types
Using a Generic Class - Example
Creating a Generic Class - Example
Raw Type, Generic Type and Parameterized Type
Parameterized Type and Subtyping
Wildcard Parameterized Type Test
Wildcard Parameterized Subtyping
►Wildcard Parameterized Subtyping Example
Generic Methods and Type Inference
Lambda Expressions and Method References
Execution Threads and Multi-Threading Java Programs
ThreadGroup Class and "system" ThreadGroup Tree
Synchronization Technique and Synchronized Code Blocks
Deadlock Condition Example Programs