Access mutable java.util.List in Clojure

tRi11 :

I'm working with a 3rd party Java library, one of the library events passes a java.util.List as a parameter which Clojure exposes as a clojure.lang.APersistentVector. The problem I'm having is that the library requires that I mutate that list to achieve the functionality I am after. However, as Clojure has returned the list as APersistentVector all the mutable methods throw an UnsupportedOperationException.

Is it possible to force Clojure to work with a mutable Java implementation of List rather than the un-mutable Clojure version?

I appreciate that this is very un-Clojure like but its the way the Library works and for now, I just want to get things working.

Thanks,

Paul

andy_fingerhut :

It isn't clear what you mean when you say "which Clojure exposes as a clojure.lang.APersistentVector". If Clojure calls a Java method that returns an object x with class java.util.ArrayList, then calling (class x) in Clojure should show java.util.ArrayList.

Is there perhaps some conversion code happening in the library you are using, perhaps a Clojure wrapper for the Java method call(s), that is doing this conversion?

Example of Clojure REPL session showing you can indeed get java.util.ArrayList objects and mutate them in Clojure code:

user=> (def mutlist1 (java.util.ArrayList. [1 2 3]))
#'user/mutlist1
user=> mutlist1
[1 2 3]
user=> (class mutlist1)
java.util.ArrayList
;; add a new element -1 at index 1
user=> (.add mutlist1 1 -1)
nil
user=> mutlist1
[1 -1 2 3]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related