HashMap using Generics


I have:

protected Map<String, ? extends Transaction> transactions = new HashMap<String, ?>();

However, I get a compilation error:

Type mismatch: cannot convert from HashMap<String,?> to Map<String,? extends Transaction>

I've tried some variations, including:

protected Map<String, ? extends Transaction> transactions = new HashMap<String, ? extends Transaction>();

All yield the same compilation error.

My goal is to have an instance of Report abstract class (extended by many different kinds of reports), accept multiple subclassess of the abstract class Transaction.

The types going into a single instance of an extended Report will all be the same type, for example TRRReport extends Report will need to accept TRRTransaction extends Transaction into it's HashMap, however TDDReport extends Report will need to accept TDDTransaction extends Transaction into it's HashMap.

How can I use a HashMap and Generics in this situation?


The point of ? in generic parameters is when you want to store a collection of an unknown type in a variable. This is called covariance.

You want a regular generic collection of Transactions:

protected Map<String, Transaction> transactions = new HashMap<String, Transaction>();


Instead of initializing it with

new HashMap<String, ?>();

You can use diamond inference:

protected Map<String, ? extends Transaction> transactions = new HashMap<>();

In that case the javacompiler will find out what to write at the constructor side itself.


  • Optional parameter in UriTemplate in WCF
  • Simple command-line app I/O in Dart
  • How to issue a command that produces infinite output and return immediately
  • Adding new column to DataFrame with values dependent on index ref
  • Objective C - Create a framework for my iphone apps?
  • Safari PHP form submission -file upload hangs
  • Cassandra: What is a subcolumn
  • How to package a jar and all dependencies within a new jar with maven
  • How to override __call__ in celery on main?
  • What is the reason that Policy.getPolicy() is considered as it will retain a static reference to the
  • AWS-SES: Handling Bounces for Invalid ISPs
  • How to convert integer to string and get length of string
  • Magento site down due to mysql error General error: 1030 Got error -1 from storage engine
  • Does Apportable support to build library binary (.a/.so)?
  • Yii2: Finding file and getting path in a directory tree
  • Clear activity stack before launching another activity
  • Suppressing passwd when calling sqlplus from shell script
  • Angular2 Response for preflight is invalid (redirect) from some GET requests
  • How do I configure context broker accept post requests from my remote sensor?
  • How can I speed up CURL tasks?
  • How can I extract results of aggregate queries in slick?
  • Display images in Django
  • Limiting recursion to certain level - Duplicate rows
  • jQuery .attr() and value
  • Spark fat jar to run multiple versions on YARN
  • Extracting HTML between tags
  • Exception “firebase.functions() takes … no argument …” when specifying a region for a Cloud Function
  • Highlight one bar in a series in highcharts?
  • Get one-time binding to work for ng-if
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Projection media query: browser support and workarounds?
  • Android screen density dpi vs ppi
  • Azure Cloud Service Web Role web pages do not load
  • Bug in WPF DataGrid
  • How to extract text from Word files using C#?
  • Calling of Constructors in a Java
  • PHP: When would you need the self:: keyword?
  • Revoking OAuth Access Token Results in 404 Not Found
  • Running Map reduces the dimensions of the matrices
  • java string with new operator and a literal