Why is a useful map helper useful? Why not just use a hashmap?

In a large java code base in my recent work, I see the code below:

public class MapHelper extends HashMap<String, Object>{

    private static final long serialVersionUID = 1L;

    public MapHelper() {
        super();
    }

    public MapHelper(MapHelper mh) {
        super(mh);
    }

    public MapHelper as_dict(String key) {
        return (MapHelper)this.get(key);
    }
}

      

I'm not sure how useful this would be. Do you have any examples that could shed some light on the usefulness of MapHelper above?

+3


source to share


3 answers


The class seems to be relatively frivolous as it is now; However:

  • This allows them to be referenced HashMap<String, Object>

    as MapHelper

    , which is shorter and ensures consistency. See also "Is there a Java equivalent or methodology for the typedef keyword in C ++?" ...

  • as_dict

    is a utility method that does casting. They seem to have a foreknowledge of what the Map contains. It is safer than executing a throw because the throw is only defined in one place. Less margin for error.

  • This allows them to add additional functionality later without updating their entire codebase.

  • Extending HashMap

    as a top-level class makes generic type arguments overridden, that is, they are available at runtime through reflection. See this blog post by Neal Gafter which explains this feature in more detail.

So there are actually quite a few small but legitimate reasons for this.



The Java API has examples like:

+6


source


It seems that the only purpose of this class is to have a shortcut.

Instead of doing

Map<String, Map<String,  Map<String,  Map<String, Object>>>> myMap = new HashMap<String, HashMap<String,  HashMap<String,  HashMap<String, Object>>>>();

      

(which is bad to read)

you can use



MapHelper myHelper = new MapHelper(new MapHelper(new MapHelper(new MapHelper())));

      

since it MapHelper

will exactly constrain generic type attributes String

andObject

Finally, imagine you want to check if ANY is equal to "1" without knowing the depth. You can call the recursive method over and over again - or implement it once on the MapHelper.

if (myHelper.contains("1"));

      

+3


source


The purpose of the class is not to hardcode you HashMap<String, Object>

for every place you want to use for this type of map (this increases the abstraction). By extending it to MapHelper, you reduce repetition and it prevents you from cluttering your code with the diamond operator.

A better name for a class would be one that describes what types of basemaps are (without being too specific):

public class StringMapHelper extends HashMap<String, Object>

      

If you do this, the class name will still be descriptive even if you end up changing the data type of the key, and again, you don't need to replace the key type everywhere in your code:

public class StringMapHelper extends HashMap<FancyString, Object>

      

0


source







All Articles