Skip to content

Collection Builtins

Himxh edited this page Oct 4, 2026 · 1 revision

集合函数速查 / Collection Built-ins Cheat Sheet

说明 / Note

本章是速查表。容器详细用法在 [List / Set / Map] 和 [Stack / Queue]。

This is a cheat sheet. Full usage is in [List / Set / Map] and [Stack / Queue].


创建 / Creation

vector()            // 空列表
set()               // 空集合
map()               // 空映射
stack()             // 空栈
queue()             // 空队列

创建函数没有参数。初始内容通过 .add(...) 添加。

Creation functions take no arguments. Add content with .add(...).


方法速查 / Method Cheat Sheet

List(vector)

方法 说明
add(x) 追加
get(i) 按下标读
remove(i) 按下标删,返回被删元素
remove(x) 按值删,返回是否成功
contains(x) 是否包含
size() 长度
isEmpty() 是否为空

Set

方法 说明
add(x) 添加(重复无效)
remove(x) 移除
contains(x) 是否包含
size() 元素个数
isEmpty() 是否为空

Map

方法 说明
put(k, v) 写入
get(k) 读取(不存在返回 null)
remove(k) 移除
containsKey(k) 键是否存在
isEmpty() 是否为空

Stack

方法 说明
push(x) 入栈
pop() 出栈
peek() 看栈顶
isEmpty() 是否为空

Queue

方法 说明
offer(x) 入队
poll() 出队
peek() 看队首
isEmpty() 是否为空

函数形式 / Function Form

容器方法也支持函数形式,参数把容器放在第一位:

Container methods also support a function form with the container first:

add(list, x)                // 等价 list.add(x)
get(map, key)               // 等价 map.get(key)
put(map, key, value)        // 等价 map.put(key, value)
size(list)                  // 等价 list.size()
contains(set, x)            // 等价 set.contains(x)

这种写法不推荐——它和"方法属于对象"的直觉不符。新代码统一用方法形式。

Not recommended — it fights the intuition that methods belong to objects. New code uses the method form.


常用速记 / Quick Notes

  • 默认用 vector。

  • Set 没有下标访问。

  • Map 没有 size()——只有 isEmpty()。

  • Stack 用 push/pop;Queue 用 offer/poll。

  • 空容器上 pop/poll/peek 会报错。

  • Default to vector.

  • Sets have no indexed access.

  • Maps have no size() — only isEmpty().

  • Stacks use push/pop; queues use offer/poll.

  • pop/poll/peek on empty containers errors.


小结 / Summary

  • 五种创建函数:vector / set / map / stack / queue。

  • 方法形式优先,函数形式兼容。

  • 具体用法见数据结构部分。

  • Five creation functions: vector / set / map / stack / queue.

  • Prefer method form; function form is legacy.

  • See the Data Structures section for details.


下一步 / Next Steps

下一章 [文件 I/O / File-IO] 讲怎么读写文件。

Next, [文件 I/O / File-IO] covers reading and writing files.


HimDSL 文档 / Documentation

入门 / Getting Started

语言基础 / Language Basics

控制流 / Control Flow

函数 / Functions

数据结构 / Data Structures

自定义类型 / Custom Types

Minecraft 集成 / Minecraft Integration

事件与调度 / Events & Scheduling

GUI 系统 / GUI System

内置函数参考 / Built-ins Reference

进阶 / Advanced

扩展 API / Extension API

你真的需要 API 吗?/ Do you really need the API?

Clone this wiki locally