0Pricing
Clojure Functional Programming & JVM Backend Development · Lesson

Working with Java Collections and Arrays

Interop often means crossing between Clojure's immutable collections and Java's mutable ones. This lesson teaches converting, iterating, and handling primitive arrays from Clojure.

Working with Java Collections and Arrays is a free Clojure Functional Programming & JVM Backend Development lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Clojure Functional Programming & JVM Backend Development learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

Is the “Working with Java Collections and Arrays” lesson free?

Yes — the full text of “Working with Java Collections and Arrays” is free to read here on the web, and the Clojure Functional Programming & JVM Backend Development course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Clojure Functional Programming & JVM Backend Development course, upgrade to CoddyKit PRO.

What will I learn in “Working with Java Collections and Arrays”?

Interop often means crossing between Clojure's immutable collections and Java's mutable ones. This lesson teaches converting, iterating, and handling primitive arrays from Clojure. You practise Clojure Functional Programming & JVM Backend Development with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Clojure Functional Programming & JVM Backend Development?

No prior experience is required. Clojure Functional Programming & JVM Backend Development on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Working with Java Collections and Arrays” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Clojure Functional Programming & JVM Backend Development lesson?

Yes. Every Clojure Functional Programming & JVM Backend Development lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Calling Java from Clojure
  2. Implementing Java Interfaces
  3. Leveraging Java Libraries & Tools
  4. Working with Java Collections and Arrays
← Back to Clojure Functional Programming & JVM Backend Development