Creating a hashmap is very simple, you just pass the generic types through - or even use a diamond notation and do it for you.
M m = new HashMap<>();
The complication is that you also want to choose the type of Card. There are several ways to do this:
You can use the Factory template and pass in a Factory object that creates cards on demand.
You can create a map outside the class and pass it in the constructor.
Have an abstract method for creating a map. When creating an instance of a class, people implement this abstract method and generate a map for it.
In the second question, there is no way to know if there is much more detail about what you are doing. This is an architectural solution and most likely will not fit into the Q and A stack overflows. It all seems a bit dirty, although you expose a lot of the internal behavior of the classes. You should probably think more about the behavior you want and the interface to provide it, rather than implementation details.
For example, you might have enum { UNSORTED, INSERTION_ORDER, etc } , and then create an instance of the right map based on this listing.
Tim b
source share