0Pricing
Clojure Functional Programming & JVM Backend Development · レッスン

Javaコレクションと配列の操作

相互運用では、Clojureの不変コレクションとJavaの可変コレクションの間を行き来することがよくあります。このレッスンでは、Clojureから変換、反復、プリミティブ配列の操作を行う方法を学びます。

「Javaコレクションと配列の操作」はCoddyKit上の無料Clojure Functional Programming & JVM Backend Developmentレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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.

よくある質問

「Javaコレクションと配列の操作」レッスンは無料ですか?

はい。「Javaコレクションと配列の操作」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応の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を演習し、24時間対応の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に戻る