HashMap in class diagram (UML)

I will build a UML 2.0 class diagram for my Java application. In my code, I have an attribute with a HashMap data type. But, as I know, there is no HashMap data type in the UML standard. The question is, can I use HashMap as the data type for a class attribute?

UPDATE

Perhaps in the diagram I should just point to the java.util package? and perhaps put this class in a diagram in a class?

+4
source share
4 answers

HashMap should not appear in your UML model. HashMap is just an implementation of a qualified association. Probably, it was simply speed that improved unskilled association. So if you have class A with a HashMap, you would model the UML class A, the UML class B, and the UML association from A to B. You can add a qualifier to the association if it is qualified with a key that is not an attribute of B. If your HashMap key is the name B (and B has that name as an attribute), you simply omit the qualifier.

To indicate the implementation of your Association (you want to implement it using a HashSet), you can add this as a keyword or create a stereotype for it (more complex).

+3
source

Just use a regular class in UML and name it HashMap . UML is a language agnostic and has no knowledge of predefined Java classes. Or didn’t I understand your question?

+1
source

HashMap is just one of many Java classes.
And you can use any java class, interface, or primitive type in the UML 2.0 class diagram.
Any data type in the Java UML 2.0 class diagram corresponds to some java class, interface, or primitive.

You are using a UML diagram to develop your own application. So feel free to distribute the UML 2.0 standard for your convenience. No one can blame you for this.

0
source

HashMap may be the Java name for the concept, but each programming language has some kind of Hash<> or Map<> class, and something equivalent should be included in UML, because many models include Hash or Map container attributes.

Some tools support the stereotype <<map>> ; if you have this, I would use it if you are mostly concerned about visual intuitiveness - but it's impossible to say which key is implied.

A qualified UML graphic device is not intuitive, and I suspect that tools for converting to anything reasonable in forward code generation are difficult. I would avoid that.

Another way to do this (which I usually do):

  • create a Hash class with V and K as common parameters. To do this correctly, K really needs to be limited to a class, such as "Ordered", also missing from UML (we always add this)
  • for every use of hash, for example. Hash<Thing, String> (be careful with the order - I use the value first, the second is the key), create a UML class named Hash<Thing,String> and an outgoing relation to Hash<> , and then match V and K with the current parameters Thing and String
  • then in the class that wants to use it, define a property, for example. things whose type is of type Hash<Thing,String> .

MagicDraw, for example, supports this.

The disadvantage of this is that you will not see an association link between the client class and the value type ( Thing in my example). The good news is that if you publish your models as programmer qualifiers, which we do, programmers see the right thing in class tables, as you can see in this example - translation_details attribute .

The difficulty in performing this basic task in UML is just one of the many problems with UML, and why most of the developers I meet today don't use it except for images on boards or documentation.

0
source

Source: https://habr.com/ru/post/1391081/


All Articles