java.util.Collections$UnmodifiableMap -> map?

887 views
Skip to first unread message

dirk

unread,
Jul 8, 2008, 12:59:47 PM7/8/08
to Clojure
Hi,

how to work with a UnmodifiableMap (e.g. from
URLConnection.getHeaderFields)?

user=> h
h
{Expires=[-1], Set-
Cookie=[PREF=ID=c929d119f5f8cfa1:TM=1215534398:LM=1215534398:S=0d4mKPsLXVPmCgaF;
expires=Thu, 08-Jul-2010 16:26:38 GMT; path=/; domain=.google.co.uk],
Date=[Tue, 08 Jul 2008 16:26:38 GMT], Content-Type=[text/html;
charset=ISO-8859-1], Server=[gws], null=[HTTP/1.1 200 OK],
Transfer-Encoding=[chunked], Cache-Control=[private, max-age=0]}
user=> (. h getClass)
(. h getClass)
class java.util.Collections$UnmodifiableMap
user=> (h "Expires")
(h "Expires")
java.lang.ClassCastException: java.util.Collections$UnmodifiableMap
java.lang.ClassCastException: java.util.Collections$UnmodifiableMap
at clojure.lang.Compiler$InvokeExpr.eval(Compiler.java:2565)
at clojure.lang.Compiler.eval(Compiler.java:3817)
at clojure.lang.Repl.main(Repl.java:75)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:
25)
at java.lang.reflect.Method.invoke(Method.java:585)
at jline.ConsoleRunner.main(ConsoleRunner.java:69)

Normal access works:

user=> ((. h get "Expires")
(. h get "Expires")
[-1]

but then it continues:

user=> ((. h get "Expires") 0)
((. h get "Expires") 0)
java.lang.ClassCastException: java.util.Collections
$UnmodifiableRandomAccessList
java.lang.ClassCastException: java.util.Collections
$UnmodifiableRandomAccessList
at clojure.lang.Compiler$InvokeExpr.eval(Compiler.java:2565)
at clojure.lang.Compiler.eval(Compiler.java:3817)
at clojure.lang.Repl.main(Repl.java:75)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:
39)
at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:
25)
at java.lang.reflect.Method.invoke(Method.java:585)
at jline.ConsoleRunner.main(ConsoleRunner.java:69)
user=>

Is iterating over the UnmodifiableCollection and generation a clojure
datastructure (e.g. map) the only solution? Am i missing something?

Cheers,
Dirk
ps: i can't post via gmail (i'll get a bounce that im not a member,
but i'm pretty sure i joined). Anyone encountered this?

Christophe Grand

unread,
Jul 8, 2008, 1:10:11 PM7/8/08
to clo...@googlegroups.com
dirk a écrit :

> Is iterating over the UnmodifiableCollection and generation a clojure
> datastructure (e.g. map) the only solution? Am i missing something?
>
you can use get with a map and nth with a list:

user=> (get (doto (java.util.HashMap.) (put "?" "hoho")) "?")
"hoho"
user=> (nth (doto (java.util.ArrayList.) (add "hoho")) 0)
"hoho"

or if you want to convert them to clojure's data structures:
user=> (into {} (doto (java.util.HashMap.) (put "?" "hoho")))
{"?" "hoho"}
user=> (into [] (doto (java.util.ArrayList.) (add "hoho")))
["hoho"]

HTH

Christophe
--
Clojure and me http://clj-me.blogspot.com/

Reply all
Reply to author
Forward
0 new messages