# Io / Github / Biezhi / Java11 / Collections

This package contains small, self-contained Java 11 collection API demonstrations. It appears to exist as a learning module: one class shows the immutable collection factory methods added in later Java versions, and the other shows how the diamond operator simplifies generic anonymous inner classes.

Together, the examples focus on readability and concise object construction rather than building reusable application infrastructure. The code is useful for onboarding because it shows the syntax and runtime behavior of the APIs in a minimal setting.

## Overview

The module is made up of two example classes:

- [`Example`](src/main/java/io/github/biezhi/java11/collections/Example.java) demonstrates the immutable collection factories on `List`, `Map`, `Map.Entry`, and `Set`.
- [`DiamondOperatorExample`](src/main/java/io/github/biezhi/java11/collections/DiamondOperatorExample.java) demonstrates a generic abstract handler and anonymous subclass creation with the diamond operator.

The examples are intentionally simple: they construct collections or handlers, print a few messages, and stop. There are no external dependencies, no shared state, and no package-level integration points beyond standard JDK APIs.

## Key Classes and Interfaces

### `Example`

Source: [`src/main/java/io/github/biezhi/java11/collections/Example.java`](src/main/java/io/github/biezhi/java11/collections/Example.java)

This class is a compact tour of the immutable collection factory methods. Its main responsibility is to show how to create empty and populated `List`, `Map`, `Map.Entry`, and `Set` instances using the `of` and `ofEntries` APIs.

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

The `main` method is the entire example. It does not return a value and has no side effects other than local variable creation.

What it does:

- Creates an empty raw `List` with `List.of()`.
- Creates a `List<String>` using `var` and `List.of(...)`.
- Creates an empty raw `Map` with `Map.of()`.
- Creates a `Map<Integer, String>` using `Map.of(...)` with alternating key/value arguments.
- Creates a `Map<Integer, String>` using `Map.ofEntries(...)` and `Map.entry(...)` for clearer multi-entry construction.
- Creates a `Map.Entry<String, String>` directly with `Map.entry(...)`.
- Creates an empty `Set<String>` with `Set.of()`.
- Creates a populated `Set<String>` with `Set.of(...)`.

Important details:

- The empty `List.of()`, `Map.of()`, and `Set.of()` calls infer `Object` element types when assigned to raw variables.
- The example intentionally mixes raw and parameterized variables so readers can see the type inference behavior and the differences in declaration style.
- All created collections are immutable factory products from the JDK. The code does not attempt mutation, which reinforces the intended usage pattern.

### `DiamondOperatorExample`

Source: [`src/main/java/io/github/biezhi/java11/collections/DiamondOperatorExample.java`](src/main/java/io/github/biezhi/java11/collections/DiamondOperatorExample.java)

This class demonstrates a generic abstract helper and the use of the diamond operator when instantiating an anonymous subclass. It appears to exist to show how Java 9-era improvements remove verbosity when working with generic anonymous classes.

#### `MyHandler<T>`

`MyHandler` is a static nested abstract class that carries a single typed value and requires subclasses to implement `handle()`.

Design role:

- It acts as a minimal generic base class for examples.
- It stores the content passed to the constructor so that subclasses can inspect it later.
- It enforces a callback-style contract through the abstract `handle()` method.

#### `MyHandler(T content)`

Constructor behavior:

- Stores the provided content in a private field.
- Prints a message to standard output immediately, including `content.toString()`.

Parameters:

- `content` - the value to wrap and retain.

Side effects:

- Writes a constructor message to the console.

Important note:

- Because the constructor calls `content.toString()`, passing `null` would throw a `NullPointerException`. The example always passes non-null values.

#### `getContent()`

Returns the wrapped `T` value.

- Return type: `T`
- Side effects: none

This method is the main accessor used by anonymous subclasses inside `main()`.

#### `setContent(T content)`

Updates the wrapped value.

- Parameter: `content` - the new value to store
- Return type: `void`
- Side effects: mutates the internal `content` field

The example does not call this method, but it completes the mutable holder pattern.

#### `handle()`

An abstract method that subclasses must implement.

- Return type: `void`
- Side effects: defined by subclasses

This method makes `MyHandler` useful as a template for small behavior-specific callbacks.

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

The `main` method demonstrates three anonymous subclasses of `MyHandler`.

What it does:

1. Creates `MyHandler<Integer>` with `new MyHandler<>(1) { ... }` and prints a message using `getContent()`.
2. Prints a separator line.
3. Creates `MyHandler<? extends Integer>` with `new MyHandler<>(10) { ... }` and again prints the content.
4. Prints another separator line.
5. Creates `MyHandler<?>` with `new MyHandler<>("魔法师") { ... }` and prints a message using the string content.

Why it matters:

- The diamond operator removes the need to repeat the generic type arguments on the constructor side.
- The anonymous subclasses still retain type safety through the declared variable type.
- The wildcard examples show that the same base class works with both numeric and non-numeric payloads.

## How It Works

### Collection factory flow

The `Example` class follows a straightforward pattern:

1. Declare a local variable.
2. Use a static factory method on `List`, `Map`, `Map.Entry`, or `Set`.
3. Let the JDK return an immutable implementation.
4. Stop without mutating the result.

This pattern emphasizes concise construction:

- `List.of(...)` for lists
- `Map.of(...)` for small maps with alternating key/value arguments
- `Map.ofEntries(...)` for maps that are easier to read as entry objects
- `Map.entry(...)` for a single entry value
- `Set.of(...)` for sets

The code is useful as a syntax reference because it shows the different shapes of the APIs side by side.

### Generic handler flow

`DiamondOperatorExample` uses a small template pattern:

1. Construct a `MyHandler<T>` subclass with an initial value.
2. The superclass constructor stores the value and prints a message.
3. The anonymous subclass implements `handle()`.
4. `handle()` reads the value through `getContent()` and prints a type-specific message.

The example is not about business logic; it is about how generic base classes and anonymous subclasses work together with the diamond operator.

## Relationships

```mermaid
flowchart TD
  ExampleMain["Example.main"] --> ListOf["List.of"]
  ExampleMain --> MapOf["Map.of"]
  ExampleMain --> MapOfEntries["Map.ofEntries"]
  ExampleMain --> SetOf["Set.of"]
  ExampleMain --> MapEntry["Map.entry"]
  DiamondMain["DiamondOperatorExample.main"] --> MyHandler["MyHandler<T>"]
  MyHandler --> Handle["handle()"]
```

## Dependencies and Integration

This module depends only on the Java standard library.

### JDK APIs used

- `java.util.List`
- `java.util.Map`
- `java.util.Set`
- `Map.entry(...)` and `Map.ofEntries(...)`
- `List.of(...)`, `Map.of(...)`, and `Set.of(...)`
- `var` local variable type inference in `Example`

There are no project-internal dependencies, service calls, or framework integrations in the two source files.

## Notes for Developers

- The collection examples use immutable factory methods. If you need a mutable list, map, or set, these examples are not a substitute for `ArrayList`, `HashMap`, or `HashSet`.
- The raw variable declarations in `Example` are intentional for demonstration, but production code should normally prefer parameterized types.
- `DiamondOperatorExample.MyHandler` prints from its constructor, so subclass instantiation has visible side effects immediately.
- `MyHandler` assumes `content` is non-null because it calls `toString()` in the constructor.
- The anonymous subclasses in `main()` are the key learning point: the diamond operator can be used even when the implementation is provided inline.

## Source References

- [`src/main/java/io/github/biezhi/java11/collections/DiamondOperatorExample.java`](src/main/java/io/github/biezhi/java11/collections/DiamondOperatorExample.java)
- [`src/main/java/io/github/biezhi/java11/collections/Example.java`](src/main/java/io/github/biezhi/java11/collections/Example.java)
