Class diagrams: generics and collection types
简介 / 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 | @startuml |
PlantUML natively supports <T> and <K,V> generic syntax in class names and method signatures, and implements naturally carries generic parameters.
Mermaid 版
1 | classDiagram |
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.