Clojure Functional Programming & JVM Backend Development · 课时

使用 Java 集合与数组

互操作通常意味着在 Clojure 的不可变集合与 Java 的可变集合之间转换。本课程将教您在 Clojure 中转换、遍历集合,并处理基本类型数组。

第 4 / 4 课13 个步骤

使用 Java 集合与数组 是 CoddyKit 上的免费 Clojure Functional Programming & JVM Backend Development 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Clojure Functional Programming & JVM Backend Development 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Clojure Functional Programming & JVM Backend Development 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Two Collection Worlds

Clojure collections are immutable and persistent; Java collections are typically mutable. Interop requires moving values between them carefully.

Java Collections Are Seqable

Most Java collections implement Iterable, so Clojure's seq functions work on them directly.

(def jlist (java.util.ArrayList. [1 2 3]))
(println (map inc jlist))

Building a Java List

Construct and mutate a Java list using interop method calls.

(let [l (java.util.ArrayList.)]
  (.add l "a")
  (.add l "b")
  (println l))

Converting to a Clojure Vector

Wrap a Java collection in vec or into to get an immutable Clojure structure.

(def jlist (java.util.ArrayList. [10 20 30]))
(println (vec jlist))
(println (into [] jlist))

Clojure Map to Java Map

A Clojure map is already a java.util.Map, so it can be passed to Java APIs expecting one. For a mutable copy, build a HashMap.

(def m {"k" 1})
(def jmap (java.util.HashMap. m))
(.put jmap "k2" 2)
(println jmap)

Primitive Arrays

Java APIs often want primitive arrays. Create them with typed constructors like int-array and double-array.

(def arr (int-array [1 2 3 4]))
(println (alength arr))
(println (aget arr 2))

Reading and Writing Arrays

Use aget to read and aset to write array slots by index.

(def arr (int-array 3))
(aset arr 0 99)
(println (vec arr))

Arrays Back to Seqs

Wrap an array in seq or vec to process it with Clojure's sequence functions.

(def arr (double-array [1.5 2.5 3.5]))
(println (reduce + (seq arr)))

Type Hints for Performance

Add type hints so the compiler avoids reflection on hot array loops.

(defn sum-ints [^ints xs]
  (areduce xs i acc 0 (+ acc (aget xs i))))

Beware Mutation Aliasing

When you hand a mutable Java collection to other code, both sides see mutations. Copy into an immutable Clojure structure when you need isolation.

Choose the Boundary

Keep mutable Java collections at the interop boundary and convert to immutable Clojure data as early as possible, so the rest of your code stays purely functional.

Quick Check

Test your Java collection interop knowledge.

Recap

You learned that Java collections are seqable, how to convert between Java and Clojure collections, build and read primitive arrays, add type hints, and keep mutation at the interop boundary.

免费开始

用 AI 导师学习 Clojure — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
12
课程
48

常见问题解答

「使用 Java 集合与数组」课时是免费的吗?

是的 — 「使用 Java 集合与数组」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Clojure Functional Programming & JVM Backend Development 课程的其余内容,请升级到 CoddyKit PRO。 Clojure Functional Programming & JVM Backend Development 课程共包含 4 节课。

「使用 Java 集合与数组」这节课中我会学到什么?

互操作通常意味着在 Clojure 的不可变集合与 Java 的可变集合之间转换。本课程将教您在 Clojure 中转换、遍历集合,并处理基本类型数组。 你通过在浏览器中直接运行的动手代码来练习 Clojure Functional Programming & JVM Backend Development,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Clojure Functional Programming & JVM Backend Development 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Clojure Functional Programming & JVM Backend Development 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「使用 Java 集合与数组」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Clojure Functional Programming & JVM Backend Development 课中编写并运行代码吗?

能。每节 Clojure Functional Programming & JVM Backend Development 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 从 Clojure 调用 Java
  2. 实现 Java 接口
  3. 利用 Java 库与工具
  4. 使用 Java 集合与数组
← 返回 Clojure Functional Programming & JVM Backend Development