← 返回目录

第四章:集合与泛型

List、Map、Stream

1. List 与 Map

List<String> names = new ArrayList<>();
names.add("alice");

Map<String, Integer> scores = new HashMap<>();
scores.put("bob", 90);

2. Stream API

var sum = List.of(1, 2, 3, 4).stream()
    .filter(n -> n % 2 == 0)
    .mapToInt(Integer::intValue)
    .sum();

📋 本章要点

使用接口类型声明集合;Stream 用于声明式处理链;注意并行流的线程安全。

评论加载中...