Which Clojure methods can I use to create multiple instances on an object and store then in an Java ArrayList?
I know how to do this in Java but I'm not sure how to proceed in Clojure, any help/pointers will be much appreciated
Look at http://clojure.org/java_interop
(doto (new java.util.ArrayList)
(.add (new Object))
(.add (new Object)))
returns #<ArrayList [java.lang.Object@5ae7fa2, java.lang.Object@33d6798]>
There are 2 forms for creating new objects in clojure.
(Classname. args*)
(new Classname args*)
So here is the straightforward example how to create the java object in clojure. Firstly how it looks in Java:
Thread thread = new Thread("Hi there");
Clojure
; return instance of java.lang.Thread class
(new Thread "Hi there")
or another way
(Thread. "Hi there")
(new Object). That's how you create an object.why not store in vector?
user=> (def lst (atom []))
user=> (swap! lst conj "String")
user=> (swap! lst conj 123)
user=> @lst
["String" 123]
lst pointing to a const empty vector. Then it creates a const vector containg the string "String" and makes lst point to that. Then it creates a const vector containing "String" and 123 and makes lst point to that. Then it dereferences the pointer. Note that this is not how you usually do things in Clojure.