Search code examples
javaumlclass-diagram

How you write collections (arrayList, vector, etc) as attribute in UML (Class Diagram)?


I want to ask how I write collections (arrayList, vector) as attribute inside class diagram?

Because when you want to add variable as an attribute you can write it like "+ name: string", but how does it work with vector? Thanks.


Solution

  • See this tutorial on “multiplicity” and “multiplicity element” in a UML diagram.

    Cardinality

    Follow the attribute name and type with an interval of one or two numbers in square brackets joined by 2 dots.

    For exactly one:

    goalKeeper : Player [1..1]
    

    …or:

    goalKeeper : Player [1]
    

    For multiples, two to three:

    forwards : Player [2..3]
    

    Use asterisk for open-ended.

    • Zero or more: [0..*]
    • One or more: [1..*]

    Must be zero: [0..0] or [0]

    Collection properties

    Follow the multiplicity-range with characteristics of:

    • ordered or unordered (meaning sorted)
    • unique or nonunique (meaning distinct)

    Nest those values in curly braces, separated by comma.

    Java

    You said:

    I write collections (arrayList, vector)

    By the way, never use Vector. That legacy class was supplanted many years ago by ArrayList, as noted in the Javadoc.

    Let's look at how the Java Collections Framework fits these properties.

    A Java Set would be { unordered, unique }. A Java SortedSet or NavigableSet would be { ordered, unique }. A Java List would be { ordered , nonunique }. As to the last combination { unordered , nonunique }, no such interface nor implementation is bundled with Java; see the 3rd-party solution, Multiset in Guava.

    Table showing a Java collection class for each combination of ordered/unordered with unique/nonunique UML multiplicity property.

    Example

    Back to your example of some entity having names, say one or more names listed in order of preference:

    + names: string [1..*] { ordered , unique }
    

    Your attribute name should likely be plural, such as names here rather than singular name.