1

In my example I deal with a lot of Map<String,Map<String,Object>> structures.

I want to replace the unwieldy syntax with something simpler. So I tried:

interface Bob extends Map<String,Map<String,Object>> {}

Then I want to do:

private Map<String,Map<String,Object>> returnBob() {
    // generate a map of strings to maps of strings to objects
    Map<String,Map<String,Object>> foo = new HashMap<String,Map<String,Object>>;
    // and so on
    return foo;
}

private void assignBob() {
   Bob theBob = returnBob();
}

but I get an IDE alert of

Type mismatch: cannot convert from Map<String,Map<String,Object>> to Bob

How can I simplify this unwieldy structure syntax?

Keith Tyler
  • 719
  • 4
  • 18
  • Even though `HashMap` is a sub type of *Map* it is not a sub type of *Bob*. You'll need to create a class which extends *HashMap* and implements *Bob* and change the signature of *returnBob* method to return a *Bob* instead of a *Map* –  Nov 26 '18 at 23:58
  • @SotiriosDelimanolis - You're right. It's closely-related, but not quite the same. – T.J. Crowder Nov 27 '18 at 00:03
  • This aliasing trick will only work if you use the new type name everywhere (and really for classes, not interfaces). IMO don't do it, just use the full name or create proper abstractions (class that encapsulates a map). – Sotirios Delimanolis Nov 27 '18 at 00:15

0 Answers0