---
title: "One-to-many relationship using collections"
subject: "Object Oriented Analysis and Design"
chapter: 4
key: "semester_6/object_oriented_analysis_and_design:one-to-many-collection-generic-classes"
source: https://www.bctengineer.com/q/semester_6/object_oriented_analysis_and_design/one-to-many-collection-generic-classes
site: BCT Engineering
---

# One-to-many relationship using collections

Object Oriented Analysis and Design · Chapter 4: Implementation

- Asked in: 2072 Asoj, 5 marks
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_6/object_oriented_analysis_and_design/one-to-many-collection-generic-classes
- Whole chapter: https://www.bctengineer.com/md/semester_6/object_oriented_analysis_and_design/chapter-4

## Question

During object oriented implementation of design class diagram you may encounter one-to-many relationships between classes. With the help of collection and generic classes, explain how you can represent these relationships in object oriented programming.

## Short answer

A one-to-many association is represented on the ‘one’ side with a collection (List, Set, Map) of references to the ‘many’ side. Generic classes (e.g., List<Employee>) provide compile-time type safety, avoid casting, and dynamic sizing. Use List for ordered, Set for unique, Map for key lookup; keep bidirectional consistency and return unmodifiable views.

## Answer

A **one-to-many** association in a class diagram (for example, one `Department` has many `Employee`s) cannot be stored in a single reference. The "one" side must hold a **collection** of references to the "many" side.

```text
 +------------+ 1       * +----------+
 | Department |-----------| Employee |
 +------------+           +----------+
 Department has List<Employee>
```

#### Collections and generics
- The Java Collections Framework (`List`, `ArrayList`, `Set`, `HashSet`, `Map`) and its C# counterparts (`List<T>`, `Dictionary<K,V>`) provide dynamic containers that grow as needed (unlike fixed-size arrays).
- **Generic classes** (`List<Employee>`) fix the element type at compile time, so no casting is needed and wrong types are rejected by the compiler (type safety).
- Choice: use `List` if order matters or duplicates are allowed, `Set` if elements must be unique, and `Map<K,V>` if lookup by key (e.g. by employee id) is needed.

#### Implementation

```java
import java.util.*;

class Employee {
    private String name;
    private Department dept;          // optional back reference (many-to-1)
    public Employee(String name) { this.name = name; }
    public String getName() { return name; }
    void setDept(Department d) { dept = d; }
}

class Department {
    private String name;
    private List<Employee> employees = new ArrayList<>();   // 1 -> *
    public Department(String name) { this.name = name; }

    public void addEmployee(Employee e) {
        employees.add(e);
        e.setDept(this);               // keep both ends consistent
    }
    public void removeEmployee(Employee e) {
        if (employees.remove(e)) e.setDept(null);
    }
    public List<Employee> getEmployees() {
        return Collections.unmodifiableList(employees);  // protect internals
    }
}
```

Points:
- The collection is created in the constructor or field initialiser (multiplicity `0..*` allows an empty list).
- For `1..*`, the constructor should require at least one element.
- Maintain both ends if the association is bidirectional.
- Return an unmodifiable view or a copy to preserve encapsulation.
- For a bounded multiplicity such as `0..30`, check the size in `add...` and throw an exception when it would exceed the limit.

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. How does a Department with many Employees represent the one-to-many association?
   A. A single Employee reference
   B. A collection such as List<Employee> held by Department
   C. An array of Department in Employee only
   D. A static integer
   Answer: B. One reference cannot hold many objects, so the 'one' side holds a collection of the 'many' side. (Key point: Collection for many side)

2. What does using a generic class like List<Employee> give?
   A. Element type fixed at compile time, no casting, wrong types rejected by the compiler
   B. Unlimited mixed types
   C. Fixed size
   D. Slower code only
   Answer: A. Generics provide type safety by fixing the element type at compile time. (Key point: Benefit of generics)

3. Which collection should be chosen when elements must be unique?
   A. List
   B. Set
   C. Array of fixed size
   D. Map only
   Answer: B. Set for uniqueness; List when order or duplicates matter; Map for lookup by key. (Key point: Choosing collection)

4. Why does getEmployees() return Collections.unmodifiableList(employees)?
   A. To make the list faster
   B. To protect the internal collection so callers cannot change it directly
   C. To sort the list
   D. To remove back references
   Answer: B. Returning an unmodifiable view or copy preserves encapsulation. (Key point: Encapsulation of collection)

## Related in Object Oriented Analysis and Design

- [Create classes from design class diagrams](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Aclasses-from-design-class-diagram) — Object Oriented Analysis and Design, chapter 4, asked 2×
- [Four class relationships from figure](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Aclass-relationships-from-figure) — Object Oriented Analysis and Design, chapter 2
- [Marina class diagram implementation code](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Amarina-class-diagram-implementation-code) — Object Oriented Analysis and Design, chapter 4
- [Video rental shop class diagram](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Avideo-rental-class-diagram) — Object Oriented Analysis and Design, chapter 2
- [XYZ Marina class diagram](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Amarina-class-diagram) — Object Oriented Analysis and Design, chapter 2
- [Object oriented system and core concepts](https://www.bctengineer.com/question.md?key=semester_6%2Fobject_oriented_analysis_and_design%3Aoo-system-concepts-explained) — Object Oriented Analysis and Design, chapter 1

## Related in other subjects

- [Compare procedural and object oriented programming](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aprocedural-vs-oop-compare) — Object Oriented Programming, chapter 1
- [Vector class addition by object argument](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Avector-class-addition) — Object Oriented Programming, chapter 1
- [Array class template with max, min, sort](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Aclass-template-array-max-min-sort) — Object Oriented Programming, chapter 9
- [Add objects of two different classes](https://www.bctengineer.com/question.md?key=semester_3%2Fobject_oriented_programming%3Afriend-add-two-class-objects) — Object Oriented Programming, chapter 4, asked 2×
