# Io / Github / Biezhi / Java11 / Collections

## Overview

This module appears to be a small set of Java 11 collection examples focused on immutable factory methods and the diamond operator in anonymous inner classes. It exists as an educational package: each class demonstrates a specific language or library capability rather than implementing application business logic.

The code is centered around two standalone example programs: one showing `List.of`, `Map.of`, `Map.ofEntries`, and `Set.of`, and another showing how Java 11 supports the diamond operator when instantiating anonymous generic classes. Together they illustrate the style and constraints of modern collection creation in Java 11.

## Key Classes and Interfaces

### [Example](java11-examples-master/src/main/java/io/github/biezhi/java11/collections/Example.java)

This class is the primary collections demonstration entry point. Its `main` method creates immutable lists, maps, and sets using the factory methods introduced in newer Java releases, and it shows the newer `var` syntax alongside those APIs.

The class does not expose reusable helpers or state. Instead, it serves as a compact reference for how to construct collection instances without mutable boilerplate.

#### `main(String[] args)`

- **What it does:** Creates several immutable collections and map entries.
- **Parameters:** Accepts the standard `String[] args`, but does not use them.
- **Returns:** `void`.
- **Side effects:** Declares local variables and demonstrates collection factory methods; no values are returned or persisted.
- **Notable behavior:**
  - `List.of()` creates an empty immutable list.
  - `List.of("biezhi", "github", "王爵的技术小黑屋")` creates a typed list inferred with `var`.
  - `Map.of()` creates an empty immutable map.
  - `Map.of(2017, "先赚他一个亿", 2018, "去年的梦想可能有点儿夸张")` creates a small immutable map with inline key/value pairs.
  - `Map.ofEntries(...)` demonstrates building a map from explicit `Map.Entry` instances.
  - `Map.entry("biezhi", "emmmm")` creates a single immutable entry that can be passed to `Map.ofEntries`.
  - `Set.of()` and `Set.of(...)` demonstrate immutable set creation.

### [DiamondOperatorExample](java11-examples-master/src/main/java/io/github/biezhi/java11/collections/DiamondOperatorExample.java)

This class demonstrates the diamond operator in the context of an anonymous generic inner class. The example is structured around a generic abstract handler that stores content and forces subclasses to implement a `handle()` method.

The design shows why the diamond operator matters: it reduces repetition when instantiating generic anonymous classes, while still preserving type inference for the handler content.

#### `MyHandler<T>`

`MyHandler` is a nested abstract base class used to carry typed content into anonymous handler implementations.

- **Role:** Provides a generic wrapper around a value plus a `handle()` contract.
- **Why it exists:** It gives the example a realistic place to use the diamond operator with anonymous subclassing.
- **Relationship to other code:** All examples in `main` create anonymous subclasses of `MyHandler`.

##### `MyHandler(T content)`

- **What it does:** Stores the provided content and prints a constructor message showing the value.
- **Parameters:** `content`, the typed payload to retain.
- **Returns:** Constructor; no return value.
- **Side effects:** Writes a line to standard output immediately upon construction.

##### `getContent()`

- **What it does:** Returns the stored payload.
- **Parameters:** None.
- **Returns:** The current `T` content.
- **Side effects:** None.

##### `setContent(T content)`

- **What it does:** Replaces the stored payload.
- **Parameters:** `content`, the new value.
- **Returns:** `void`.
- **Side effects:** Mutates the internal `content` field.

##### `handle()`

- **What it does:** Declares the operation that subclasses must implement.
- **Parameters:** None.
- **Returns:** `void`.
- **Side effects:** None in the abstract base class; behavior is defined in anonymous subclasses.

#### `main(String[] args)`

- **What it does:** Creates three anonymous `MyHandler` implementations and calls `handle()` on each.
- **Parameters:** Standard `String[] args`, unused.
- **Returns:** `void`.
- **Side effects:** Prints constructor and handler messages to standard output.
- **Notable behavior:**
  - `new MyHandler<>(1) { ... }` demonstrates the diamond operator with an anonymous class that handles `Integer` content.
  - `new MyHandler<>(10) { ... }` shows the same pattern with a wildcard-typed reference (`MyHandler<? extends Integer>`).
  - `new MyHandler<>("魔法师") { ... }` shows the generic handler carrying a `String` payload.
  - Each anonymous class overrides `handle()` to print a message using `getContent()`.

## How It Works

### Collections example flow

1. `main` starts by creating an empty immutable list with `List.of()`.
2. It creates a typed list with `List.of(...)` and stores it in a `var` local.
3. It constructs immutable maps using both `Map.of(...)` and `Map.ofEntries(...)`.
4. It creates a `Map.Entry` using `Map.entry(...)` and passes it into `Map.ofEntries(...)`.
5. It creates immutable sets with `Set.of()` and `Set.of(...)`.
6. The method ends without printing or returning anything; the examples are purely illustrative.

### Diamond operator flow

1. `main` instantiates an anonymous `MyHandler<Integer>` using the diamond operator.
2. The `MyHandler` constructor stores the payload and prints a construction message.
3. The anonymous class overrides `handle()` and uses `getContent()` to read the stored value.
4. `handle()` is invoked and prints a message derived from the payload.
5. The same pattern repeats for a wildcard-typed handler and for a `String`-typed handler.

The example highlights that the diamond operator can be used even when creating anonymous inner classes, reducing duplication while keeping the generic intent clear.

## Data Model

This module does not define application entities or DTOs. The only structured data used is the collection factory output and the generic payload wrapped by `MyHandler<T>`.

- **Immutable collections:** `List`, `Map`, and `Set` instances created through factory methods.
- **Map entries:** `Map.Entry<String, String>` and entries created with `Map.entry(...)` / `Map.ofEntries(...)`.
- **Generic payload:** `MyHandler<T>` stores a single typed value in its `content` field.

## Dependencies and Integration

This package is self-contained and does not depend on other project modules. The indexed evidence shows no package dependencies or cross-module relationships for `io.github.biezhi.java11.collections`.

It does rely on standard Java collection APIs:

- `java.util.List`
- `java.util.Map`
- `java.util.Set`
- `java.util.Map.entry`

## Module Relationships

```mermaid
flowchart LR
A["io.github.biezhi.java11.collections.Example"] --> B["List.of and immutable List"]
A --> C["Map.of and Map.ofEntries"]
A --> D["Set.of and immutable Set"]
E["io.github.biezhi.java11.collections.DiamondOperatorExample"] --> F["MyHandler<T> abstract base"]
E --> G["Anonymous MyHandler implementations"]
G --> H["Diamond operator with anonymous classes"]
```

## Notes for Developers

- These classes are examples, so they intentionally keep all logic inside `main` methods.
- The collection factory methods return immutable instances. Code that expects mutation should not reuse these examples as-is.
- `Map.of(...)` is convenient for small maps, while `Map.ofEntries(...)` is better when you already have `Map.Entry` values or many pairs to assemble.
- `MyHandler<T>` prints from its constructor, which makes object creation observable in the console. That is useful for a demo, but it would be noisy in production code.
- The wildcard example `MyHandler<? extends Integer>` is read-oriented: it shows that the handler can be referenced through a covariant type, but the example only uses `getContent()` and `handle()`.
- There is no shared state, error handling, or external I/O beyond console output.
