Class diagrams: generics and collection types

Class diagrams: generics and collection types

puml.online

简介 / Introduction

Generics and collection types are core concepts in object-oriented design. In class diagrams, generics constrain the parameterized types of class members, return values, and parameters. This post uses List<T> and Map<K,V> as examples to compare PlantUML and Mermaid approaches.

PlantUML 版

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
@startuml
skinparam classAttributeIconSize 0

interface List<T> {
+add(item: T): void
+get(index: int): T
+size(): int
}

interface Map<K, V> {
+get(key: K): V
+put(key: K, value: V): void
+containsKey(key: K): boolean
}

class ArrayList<T> implements List<T> {
-data: T[]
+add(item: T): void
+get(index: int): T
}

class HashMap<K, V> implements Map<K, V> {
-buckets: Entry<K,V>[]
+get(key: K): V
+put(key: K, value: V): void
}

class Entry<K, V> {
+key: K
+value: V
}

List <|.. ArrayList
Map <|.. HashMap
@enduml

PlantUML natively supports <T> and <K,V> generic syntax in class names and method signatures, and implements naturally carries generic parameters.

Mermaid 版

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
classDiagram
class List~T~ {
<<interface>>
+add(item: T): void
+get(index: int): T
+size(): int
}

class Map~K,V~ {
<<interface>>
+get(key: K): V
+put(key: K, value: V): void
+containsKey(key: K): boolean
}

class ArrayList~T~ {
+add(item: T): void
+get(index: int): T
}

class HashMap~K,V~ {
+get(key: K): V
+put(key: K, value: V): void
}

class Entry~K,V~ {
+key: K
+value: V
}

List <|.. ArrayList
Map <|.. HashMap

Mermaid classDiagram uses ~T~ syntax (tilde-wrapped) for type parameters, with <<interface>> inside the class body to mark interfaces. Inheritance relationships work the same way.

对比 / Side-by-side

Feature PlantUML Mermaid
Generic class name class Map<K, V> class Map~K,V~
Interface marker interface List<T> <<interface>> inside class body
Generic methods +get(key: K): V +get(key: K): V
Implements relation implements `<
Member visibility + - # ~ + - # ~

小结 / Wrap-up

Both tools can express generic class diagrams. PlantUML’s syntax is closer to source code style with explicit implements keywords; Mermaid uses ~T~ notation for type parameters and falls back gracefully in environments without native generic support. PlantUML is better suited for complex enterprise class diagrams, while Mermaid integrates more easily into Markdown documents and blogs.

  • Title: Class diagrams: generics and collection types
  • Author: puml.online
  • Created at : 2026-09-04 09:00:00
  • Updated at : 2026-09-04 01:06:45
  • Link: https://puml.online/blog/class-diagram-generics-en/
  • License: This work is licensed under CC BY-NC-SA 4.0.
On this page
Class diagrams: generics and collection types