# Io / Github / Biezhi / Java11 / Collections

## Overview

This package demonstrates Java 11 collection-related conveniences through small, self-contained examples. It appears to exist as a learning and reference module: one class focuses on immutable collection factory methods such as `List.of()`, `Map.of()`, `Map.ofEntries()`, and `Set.of()`, while the other illustrates use of the diamond operator with anonymous inner classes and a generic handler type.

The code does not build a reusable collection framework. Instead, it shows the syntax, type inference behavior, and runtime output of a few Java 11-era APIs in a way that engineers can run and inspect directly.

## Key Classes and Interfaces

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

This class is a compact tour of immutable collection factories introduced in newer Java releases. Its `main` method creates empty and populated lists, maps, and sets, and it uses `var` to show how local variable type inference interacts with these factory methods.

Important details:

- `main(String[] args)` is the only method.
- It demonstrates `List.of()` for an empty list and `List.of(...)` for a populated immutable list.
- It demonstrates `Map.of()` for an empty map and `Map.of(k1, v1, ...)` for a small immutable map.
- It demonstrates `Map.ofEntries(...)` together with `Map.entry(...)` for entry-based map creation.
- It demonstrates `Set.of()` and `Set.of(...)` for empty and populated immutable sets.

The method does not return anything or preserve the created collections beyond the local scope. Its side effect is limited to allocating examples; the code is primarily instructional.

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

This class demonstrates the diamond operator in the context of a generic abstract inner class and anonymous subclasses. It shows how `new MyHandler<>(...)` can infer the generic type from the constructor argument and the target variable type.

Important details:

- `MyHandler<T>` is a static abstract nested class that stores one value of type `T`.
- The constructor accepts a `T content` value, assigns it to a field, and prints a message immediately.
- `getContent()` returns the stored value.
- `setContent(T content)` mutates the stored value.
- `handle()` is abstract and must be implemented by anonymous subclasses in `main`.
- `main(String[] args)` creates three handlers:
  - `MyHandler<Integer>` with a diamond-instantiated anonymous class.
  - `MyHandler<? extends Integer>` to show bounded wildcard usage.
  - `MyHandler<?>` with a `String` payload.

The nested class is the key design point here. It provides a minimal generic container plus a polymorphic hook (`handle`) so the example can demonstrate how the diamond operator behaves when type parameters are inferred on anonymous classes.

## How It Works

### Immutable collection factory example

The flow in [`Example`](src/main/java/io/github/biezhi/java11/collections/Example.java) is straightforward:

1. `List.of()` creates an empty immutable list and is assigned to a raw `List` variable.
2. `List.of("biezhi", "github", "王爵的技术小黑屋")` creates an immutable `List<String>` and is assigned to `var foo`.
3. `Map.of()` creates an empty immutable map.
4. `Map.of(2017, "先赚他一个亿", 2018, "去年的梦想可能有点儿夸张")` creates a small immutable map from literal key/value pairs.
5. `Map.ofEntries(...)` builds a map from `Map.Entry` objects produced via the static `entry(...)` import.
6. `Map.entry("biezhi", "emmmm")` creates one immutable entry, which is then passed into `Map.ofEntries(...)`.
7. `Set.of()` and `Set.of(...)` create empty and populated immutable sets.

A few practical observations follow from the code:

- The examples intentionally use `var` to let the compiler infer the collection types.
- The collections created here are immutable; they are suitable for demonstration, constants, or defensive snapshots, not mutation-heavy workflows.
- The code uses raw types for `immutableList` and `emptyImmutableMap`, which is fine for a demo but not ideal for production code.

### Diamond operator example

The flow in [`DiamondOperatorExample`](src/main/java/io/github/biezhi/java11/collections/DiamondOperatorExample.java) works in three steps:

1. A `MyHandler<T>` instance is created as an anonymous subclass using the diamond operator, for example `new MyHandler<>(1) { ... }`.
2. The constructor runs first, stores the content, and prints a message that includes `content.toString()`.
3. The subclass implements `handle()`, which prints behavior specific to the concrete payload.

The three handler instances show different type inference scenarios:

- `MyHandler<Integer>`: a normal parameterized type with a diamond-instantiated anonymous class.
- `MyHandler<? extends Integer>`: demonstrates that the target type can be a bounded wildcard, even though the underlying anonymous class is still built from a concrete constructor argument.
- `MyHandler<?>`: demonstrates a wildcard-typed reference with a `String` payload.

The nested class keeps the example focused on generic inference. There is no business logic beyond printing, which makes the runtime behavior easy to observe and reason about.

## Data Model

This module does not define domain entities in the usual sense. Its only real data structure is the nested generic holder:

- `MyHandler<T>` stores a single field, `content`, of type `T`.
- `content` is set in the constructor and can be changed through `setContent(T content)`.
- `getContent()` exposes the current value to subclasses and callers.

For the collections demo, the data model is the Java standard library itself:

- `List<String>` for ordered immutable values.
- `Map<Integer, String>` and `Map.Entry<String, String>` for key/value examples.
- `Set<String>` for unordered immutable values.

## Dependencies and Integration

This package depends only on the Java standard library. The import list in [`Example`](src/main/java/io/github/biezhi/java11/collections/Example.java) is limited to `java.util.List`, `java.util.Map`, `java.util.Set`, and the static import of `java.util.Map.entry`.

There are no package-level dependencies, no framework integration points, and no cross-module links recorded in the index. The classes are meant to run independently as small console examples.

## Mermaid Relationship Diagram

```mermaid
flowchart TD
CollectionsExample["io.github.biezhi.java11.collections.Example"] --> ListFactory["List.of"]
CollectionsExample --> MapFactory["Map.of / Map.ofEntries / Map.entry"]
CollectionsExample --> SetFactory["Set.of"]
DiamondOperatorExample["io.github.biezhi.java11.collections.DiamondOperatorExample"] --> MyHandler["MyHandler<T>"]
DiamondOperatorExample --> AnonymousHandlers["anonymous MyHandler implementations"]
MyHandler --> HandleMethod["handle()"]
```

## Notes for Developers

- The example code is intentionally small and illustrative. It is safe to extend, but any additions should preserve the didactic style so the sample remains easy to read.
- The collection factories in `Example` return immutable collections. If you need mutation, convert to a mutable implementation explicitly rather than assuming these instances can be updated.
- `DiamondOperatorExample` relies on anonymous subclasses, so each instance must implement `handle()`. If you add more behavior, keep the constructor side effects in mind: the constructor prints immediately when the instance is created.
- `MyHandler<T>` is abstract and nested inside the demo class, so it is not intended as a general-purpose utility type.
- The raw `List` and `Map` variables in `Example` are acceptable for a learning sample, but production code would normally keep the generic type parameters to avoid unchecked operations.

## Typical Usage

Both classes are designed to be run directly from the command line or an IDE:

- Run [`Example`](src/main/java/io/github/biezhi/java11/collections/Example.java) to see immutable list, map, and set creation.
- Run [`DiamondOperatorExample`](src/main/java/io/github/biezhi/java11/collections/DiamondOperatorExample.java) to see type inference with the diamond operator and anonymous inner classes.

Because the methods only print to standard output, the examples are best used as reference code, regression demos, or teaching material rather than as reusable application components.
