1

I am trying to see if I can use to implement a Map that can accept two different Classes using generics like the one specified in Java Generics Wildcarding With Multiple Classes

I tried

public class LocalizedStringMap extends ConcurrentHashMap<Locale & String, String> {

But that came up as a syntax error. The closest thing I can do is just make a separate method that will getByLocale() instead.

Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265
  • `public class LocalizedStringMap extends ConcurrentHashMap {`? – GBlodgett Aug 29 '19 at 22:00
  • @GBlodgett only interfaces, not classes can be extended this way – user3159253 Aug 29 '19 at 22:13
  • @ArchimedesTrajano what is your actual goal? Maybe you need a complex key like `Pair` or multiple indices like https://stackoverflow.com/questions/2501449/multiple-indexes-for-a-java-collection-most-basic-solution ? – user3159253 Aug 29 '19 at 22:52

1 Answers1

1

By design, this can't be done. Generics handles type boxing and compile-time type checking for you. Using your example, keySet() would return a Set object with both Locale and String members, with no way to enforce type checking or handle unboxing.

The example you cited only works for a single class and one or more interfaces, but does not work for more than one class.

natt
  • 127
  • 4